Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net C#4.0 [(null)] in output

Tags:

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> 
like image 658
UncleBlue Avatar asked Oct 01 '13 12:10

UncleBlue


People also ask

What is log4net used for?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

Is log4j and log4net same?

log4net is a port of the excellent Apache log4j™ framework to the Microsoft® . NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the . NET runtime.


1 Answers

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")

like image 190
Ash Burlaczenko Avatar answered Sep 22 '22 08:09

Ash Burlaczenko