I use log4net in my webapi project (using autofac and owin). I added this to my controller
LogManager.GetLogger(typeof(NotificationController));
But I have seen that there is no appender loaded. In my config I have this
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
...
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="aiAppender" />
<appender-ref ref="TraceAppender" />
<appender-ref ref="FileAppender" />
</root>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log\server.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
</log4net>
I have no build errors and my code is running without error if I call this
logger.Fatal("Error");
But I have no logs.
UPDATE
Added this to startup class but doesn't change
public class Startup
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Startup));
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
logger.Info("Start application");
Log4Net has to be told explicitly that you want it to read the XML configuration from App.config.
See: log4net only works when XmlConfigurator.Configure() is called
Either call the static method XmlConfigurator.Configure() in your application startup code, or add the following to AssemblyInfo.cs for all projects in the solution:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
You may also find that you have to call LogManager.GetLogger() immediately after startup - it seems to make a difference even if the logger instance is not used for anything.
Related
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");
}
I am using log4net for logging in my webapi. logging is working fine in my localhost or while i am deploying it on a app server. Same thing is not working in azure web service. Folder where log file has to be created is there but no files in there.
i have added below section in my web.config file,
<log4net>
<appender name ="ErrorLog" type ="log4net.Appender.RollingFileAppender">
<file value ="MyLogs\"/>
<staticLogFileName value ="false" />
<appendToFile value ="true"/>
<rollingStyle value ="Date"/>
<datePattern value ="yyyy-MM-dd.'Err'" />
<lockingModel type ="log4net.Appender.FileAppender+MinimalLock"/>
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value ="%d{DATE} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name ="ErrorLog">
<maximumFileSize value ="15MB" />
<appender-ref ref="ErrorLog"/>
</logger>
</log4net>
It seems that there are some mistake with log4net configuration.
Here I have a complete steps you could follow with and it works well on my site:
1.Install the log4net.dll binary using NuGet.
2.Configure the log4net name, type properties in the web.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
</configuration>
3.Configure the log4net properties
<log4net>
<root>
<level value="Debug"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\Home\LogFiles\Log4Net\Log4Net.log"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="3MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<logger name="SleepyCore">
<level value="INFO"/>
</logger>
</log4net>
Note: You need to specify the address of the log on azure like D:\Home
4.Modify the Global.asax Application_Start() method add the following code in the method.
log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
5.Write log with an instance of the ILog interface
ILog log = LogManager.GetLogger("SleepyCore");
log.Info("Begin - Page_Load() at " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
6.The output as below:
For more details about how to configure log4net on azure, you could refer to this article.
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.
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>