I have created a Console App (.Net Framework) project for my NSelene UI automation project. Currently, I am configuring log4net. Unfortunately, I am unable to log into an output window (log4net is writing only into a file).
Test output showing next error:
log4net:ERROR XmlHierarchyConfigurator: No appender named [DebugAppender] could be found.
log4net:ERROR Appender named [DebugAppender] not found.
I have added [assembly: log4net.Config.XmlConfigurator(Watch = true)] into AssemblyInfo.cs file
app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="DebugAppender "
type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -
%m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="log/SBT.UI.log"/>
<encoding value="utf-8"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss},
%message%newline"/>
</layout>
</appender>
<appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -
%m%n" />
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="DebugAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Project.cs
[SetUp]
public void Initialize()
{
logger.Debug("test1");
logger.Info("test2");
logger.Warn("test3");
logger.Error("test4");
logger.Fatal("test5");
}
Related
I created a a file called log4net.config in my project and added the following configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
After that, I added the line below to my AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
In the application, I have the following static class:
public static class Logger
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void LogInfo(string msg)
{
log.Info(msg);
}
public static void LogDebug(string msg)
{
log.Debug(msg);
}
public static void LogWarn(string msg, Exception e)
{
log.Warn(msg, e);
}
public static void LogError(string msg, Exception e)
{
log.Error(msg, e);
}
}
Then I'm trying to log somewhere in the application in another class using Logger.LogDebug("Error logger working.");
However, I can't see any log file being created or written to it. What am I doing wrong?
UPDATE:
I also added the following in my app.config file but I see nothing at all on my Output console...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
If you have the log4net configuration in a log4net logging configuration file, you do not have to add the configuration of the web.config (configuration tag and sections). Just add the log4net tags:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
</layout>
</appender>
</log4net>
I could not reproduce your mistake. Configure log4net in AssemblyInfo works fine. maybe the problem in configuration. let me show you another implementation
private static readonly ILog log = LogManager.GetLogger("myLogger");
private static string configFile = "log4net.config";
static Logger()
{
XmlConfigurator.Configure(new FileInfo(configFile));
}
log4net.config:
<log4net>
<logger name="myLogger">
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="RollingFileAppender" />
</logger>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
</layout>
</appender>
</log4net>
Managed to find out the issue thanks to stuartd's comment. My problem was, my full solution had 2 projects. I had only configured one project (which is not the start-up project) for log4net.
My original log4net configuration did not have any mistake. I simply added log4net reference to my start up project and added log4net configuration to that project as well and it started logging.
Many thanks to everyone who tested my config.
log file is not getting created,
using below call to get logger and config file.
is it required to give full path for file?
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Does your code call
log4net.Config.XmlConfigurator.Configure();
on startup?
The <root> section I use has priority, not level:
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
(Apparently, that's not the issue https://stackoverflow.com/a/24188507/21336)
after adding below line in AssemblyInfo.cs, could see log file getting created and logs coming.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
log4net is not creating any text file to log. I tried like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="EpinovaADSyncJob.log" />
<appendToFile value="false" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value="yyyy-MM-dd" />
<preserveLogFileNameExtension value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
In assembly file:
[assembly: log4net.Config.XmlConfigurator(
ConfigFile = "Log4Net.config", Watch = true)]
Declared as class variable
private static readonly ILog Log = LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
In a method, called the Log like this
BasicConfigurator.Configure();
Log.Debug("Myjob");
Log.Error("newlkjasdf");
Log.Info("lkjlkjasdfasdf");
Try this appender :
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
I have some NServiceBus endpoints hosted as Windows Services and a few MVC 5 web sites in the same solution. All the projects use the same Assembly configuration for log4net and the same config file (literally the same files, they are shared files). The desired output is the UDP appender output to a Log2Console session.
The windows services log everything fine, but the MVC web sites do not.
Here is the assembly config:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4netconfig.xml", Watch = true)]
And here is the content of "log4netconfig.xml":
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="9998" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="UdpAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Edit
After removing the assembly config attribute, adding a log statement as the first item in Application_Start and setting up the below in web.config, I still get no logging:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="9998" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="UdpAppender" />
</root>
</log4net>
I want to create a log text file for my mobile application.I am working on compact framework 3.5 and i chosen log4net for logging.I followed the below blog to create the Config.xml file and log file http://breathingtech.com/2009/using-apache-log4net-in-net-compact-framework-projects/ which runs without any error but not created any log file.See my Config.xml file,
<?xml version="1.0" encoding="utf-8" ?>
<!-- .NET application configuration file -->
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders
and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
I added the below line under program.cs file
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Program));
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\Config.xml";
if (System.IO.File.Exists(path))
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));
}
Log.Info("Application startup");
Do my Config.xml is correct,i just copied the content from the above blog and i am getting the Config.xml path in program.cs.
Since the above setup not created log-file.txt and not logged the log.Info message,i googled and added the below line after the using log4net.Config;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "../Config.xml", Watch = true)]
This give me an error "'log4net.Config.XmlConfigurator' is not an attribute class". Can any one help me in creating a log file in compact framework.
Thanks
Try using following log4net configurations. I use this and it generates log file.
<log4net>
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log.txt"/>
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="yyyy.MM.dd"/>
<param name="StaticLogFileName" value="true"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %-5p - %m%n"/>
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="ALL"/>
<appender-ref ref="rollingFile"/>
</root>
</log4net>
In your log4net config file you do not need to add the <configuration> root tag. <log4net> should be the root. The <configuration> is the root tag if you are putting your config in the application config file or web config file.
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!-- Define some output appenders -->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders
and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>