I have the following file Log4net.config in my bin directory:
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="file" value="MyLogFile.log"/>
<param name="appendToFile" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ConsoleAppender"/>
</root>
<logger name="NHibernate" additivity="false">
<level value="WARN"/>
</logger>
</log4net>
And the following code in my AssemblyInfo.cs file:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyTitle("My Project")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
When I run the program, I get the following log4net debug output:
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\log4net.dll]. (.NET Runtime [4.0.30319.1] on Microsoft Windows NT 6.1.7600.0)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] Loaded From [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\MyCompany.Framework.dll]
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
The thread 'vshost.RunParkingWindow' (0xd30) has exited with code 0 (0x0).
The thread '<No Name>' (0x15d0) has exited with code 0 (0x0).
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]
Log4net loads, but doesn't seem to be processing my config file. When I comment out the attribute in AssemblyInfo.cs and run the following code during my program initialization, it works as expected:
var log4netConfig = "Log4net.config";
var log4netInfo = new FileInfo(log4netConfig);
log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netInfo);
What am I doing wrong? I want to load from AssemblyInfo.cs.
I also have trouble with this method of boostrapping log4net. The documentation says you have to make a call very early in your application startup
if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.
Try placing a logger in the same class that starts your application (program.cs, app.xaml, whatever). For example
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
And for kicks, make any call to the log (even one that is filtered or evaluated out of the append process, it should force log4net to evaluate your repository/heirarchy).
static Program()
{
Log.Debug("Application loaded.");
}
finally i just find the simple solution, you may get help there
Global.asax in start function
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
}
In any of the class where use logging at class level
add namespace
using log4net;
add this code line at class level
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
use log function in any of the action call
log.Error("test error q111..");
configuration
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
</configSection>
<log4net debug="true">
<!--AdoNet appender is use for write log file into sql server-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=DESKTOP-NLH31FH; Initial Catalog=SmallBizDb;Integrated Security=true" providerName="System.Data.SqlClient" />
<commandText value="INSERT INTO [dbo].[Logs] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (#logdate,#thread, #loglevel, #logger, #message, #exception)" />
<parameter>
<parameterName value="#logdate" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="#loglevel" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="#exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
<root>
<level value="Debug" />
<!--<appender-ref ref="RollingLogFileAppender" />-->
<!--Enable this line if you want write log file into plain text file-->
<appender-ref ref="AdoNetAppender" />
<!--Enable this line if you want write log file into sql server-->
</root>
</log4net>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
it may help all and easy to use. thanks
I do keep log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
in the Global.asax.cs inside of the Application_Start()... So I don't need to carry the command all over the places.
I fixed this by adding the RepositoryAttribute to the offending assembly's AssemblyInfo.cs file.
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: RepositoryAttribute("Your.Namespace.Here")]
I am using Web.Config sections to configure Logger and manually log events, Bootstrapping Logger from global.asax
static ILog logger = LogManager.GetLogger(<LoggerName>);
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
}
Try bootstrapping it from global.asax
I was using log4Net with windows service. I tried all the possible options mentioned in the other answers.
For me, another instance of the service was left alive which I had to kill from the task manager. After this, the issue was resolved.
var log4NetPath = Server.MapPath("~/log4net.config");
FileInfo fileInfo = new FileInfo(log4NetPath);
XmlConfigurator.ConfigureAndWatch(fileInfo);
Related
I am using log4net to log the error in my application.But the log file is not getting created.As far as I can see,the code is fine.
Web.config code
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Demo\example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
In the AssemblyInfo.cs file,I have added the following code
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
In the class where I want to track the errors,I have added the following code
private static readonly ILog logger = LogManager.GetLogger(typeof(ProviderRepository));
I am using the logger as so in the catch block.
logger.Error(Ex.Message);
But I dont see the Log file.Am I doing something wrong here?
You should check if your apppool user has access to C:\Demo\, next you can enable log4net internal debugging which will tell you if there is any problem in your configuration or file access etc:
<configuration>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net FAQ
Thanks a lot for all your suggestions guys.I was able to figure this one out.I had write the below line of code in the Global.asax file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I was writing it earlier in the Assembly.info.cs file.
When I use log4net for logging in .NET4.0 I use the following code
LogManager declaration:
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
And when used in code:
log.Info("My info text");
This outputs the following to the log-file:
2013-10-01 14:41:11,010 [3] INFO MyNamespace.MyClass [(null)] - My info text
I am wondering what the [(null)] means and how I can remove/change it?
This is my config-file:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.txt" />
<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="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Fix
To stop it appearing in the log remove [%property{NDC}] from the config file.
This happens because log4net automatically populates a context of information that might be useful for logging - in some cases this might result in a null value.
log4net NDC
This post explains how to use NDC: When to use 'nested diagnostic context' (NDC)?
In short log4net calls the static method log4net.NDC.Push to set the context of the the code.
You could for example set the NDC manually by running the following:
log4net.NDC.Push("context")
From that point forward outputting a log message will supply %property{NDC} as the string context.
UPDATE:
NDC.Push has been deprecated. Alternatively, you can use ThreadContext.Stacks["NDC"].Push("context")
if you just want the (null) to disappear you can also set log4net.Util.SystemInfo.NullText to some other value like "" or string.Empty, it will use that instead.
I have this as my config file:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\\logging\\EwsSearch.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
<root>
<level value="ERROR" />
<appender-ref ref="RollingFileAppender" />
</root>
</configuration>
I then have in my C# code:
public class VsiEWSSearch
{
private static readonly ILog logger = LogManager.GetLogger(typeof(VsiEWSSearch));
public EWSResponse PdeProcessInquiry(int BusinessLineCode, int ClientCaseId, string callingApp)
{
XmlConfigurator.Configure(new System.IO.FileInfo("VSI.EWSSearch.config"));
logger.Error("This is an error");
No log file is produced even though I have adjusted permissions for the directory. What's wrong?
The root node has to be inside the log4net node. Depending on the type of configuration file you need to make some further adjustments:
If you have a standalone config file for log4net then you need to remove the configuration and configSections node. I also think that log4net is not going to find you configuration file unless you specify the full path (you can of course do that without hard-coding any path in your application).
If you are simply using app.config then you do not need further modifications, but you need to call the XmlConfigurator.Configure() method without any argument.
Note: You should call the XmlConfigurator.Configure() method only once in your application.
Below is my config and the output from tracing, it just doesn't configure the logger and when I use it nothing is written to the log (probably because all log levels are not enabled).
What could be the problem?
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<log4net>
<!-- Define some output appenders -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\rolling-log.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100" />
<rollingStyle value="Size" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]
" />
<footer value="[Footer]
" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="logs\bidz-log.txt" />
<!-- Example using environment variables in params -->
<!-- <file value="${TMP}\log-file.txt" /> -->
<sppendToFile value="true" />
<!-- An alternate output encoding can be specified -->
<!-- <encoding value="unicodeFFFE" /> -->
<layout type="log4net.Layout.PatternLayout">
<geader value="[Header]
" />
<footer value="[Footer]
" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
<!-- Alternate layout using XML
<layout type="log4net.Layout.XMLLayout" /> -->
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<!-- Specify the level for some specific categories -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<debug value="true" />
<logger name="Presence">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
--- debug
log4net: log4net assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=null]. Loaded from [C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\presenceapproval\4fbf2110\7bd5e802\assembly\dl3\8d349422\73aaf263_e5adcc01\log4net.DLL]. (.NET Runtime [2.0.50727.3615] on Microsoft Windows NT 5.1.2600 Service Pack 3)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [App_Web_zceiak6m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [App_Web_zceiak6m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\presenceapproval\4fbf2110\7bd5e802\App_Web_zceiak6m.dll]
log4net: Assembly [App_Web_zceiak6m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [App_Web_zceiak6m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
okay, what I needed to do was to actually call one of the configure methods first. for example: log4net.Config.XmlConfigurator.Configure(); anyway I moved the config to a separate file and added the following to web.config:
<appSettings>
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>`
I have a console application for which I am trying to setup logging with log4net.
I have done all the basic steps for setting it up -
Added Configuration in App
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="LogFileRootFolderPath" value="C:\TestApplicationLogs\"/>
</appSettings>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="TestApplication.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="14" />
<maximumFileSize value="15000KB" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="{%level}%date{MM/dd HH:mm:ss} - %message%newline"/>
</layout>
</appender>
<root>
<level value="All" />
<!-- You can use levels, in increasing order of verbosity: Off, Fatal, Error, Warn, Info, Debug, All -->
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
P.S. I have created a new folder 'TestApplicationLogs' in C:\
Added [assembly: log4net.Config.XmlConfigurator(Watch = true)] in AssemblyInfo.cs
In Program.cs
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log.Info("App Started");
}
Can anyone please guide what is missing?
I don't see where you define that C:\TestApplicationLogs as the directory where it should log to.
In the log4net config you have just specified the file name, so it will log to the directory where you execute your exe.