Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net - How to add a 2nd logger used only for specific sections of code

I'm using Log4Net 2.0, I have it working as required but which to add another logger for specific log statements. All logging is done to a single file appender. The change I want to make is that a 3rd party application fires events containing debug/error information, I can catch this information and whilst useful I feel it pollutes the normal application log file, and would instead prefer this to be stored in its own log file.

The end result I would like is a file called log-file.txt with all the logging from the application except the 3rd party logging. And a 2nd file called log-file-3rdparty.txt with logging only from the 3rd party application. The problem I have is setting up Log4Net to have 2 seperate loggers. I have already tried creating a 2nd LogFileAppender and adding it to the root however all this does in puts the same logging statements into both loggers.

Across the application we currently have the GetLogger statement as follows. This ideally needs to stay the same, so existing logging is unchanged. I need a new logger that is not affected by this statement, but is instead instantiated purely for 3rd party logging.

private readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And the App.Config as follows.

< log4net> <logger name="MyApp.Logging">   <level value="DEBUG"/> </logger>  <root>   <level value="ALL" />   <appender-ref ref="LogFileAppender" /> </root>  <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >   <param name="File" value="log-file.txt" />   <param name="AppendToFile" value="true" />   <layout type="log4net.Layout.PatternLayout">     <param name="Header" value="&#13;&#10;[Application started]&#13;&#10;" />     <param name="Footer" value="[Application Finished]&#13;&#10;"/>     <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />   </layout> </appender> </ log4net> 

Can you help?

EDIT

If it makes any difference. The 3rd party log events are captured in the same class that some of my application log events are also fired from, this is because the class is responsible for controlling the 3rd part software. Is this an issue? I do have some inkling that log4net can differentiate which logger to create based on class names, and if that's the case do i need to refactor the 3rd party logging to its own class?

like image 356
JonWillis Avatar asked Mar 01 '11 11:03

JonWillis


People also ask

How do I use multiple Appenders in log4net?

You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.

What is RollingFileAppender in log4net?

RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.

Is log4net structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.


1 Answers

You can easily create a special logger like this:

ILog specialLogger = LogManager.GetLogger("SpecialLogger"); 

You can then configure the system to use a dedicated appender for this logger:

<logger name="SpecialLogger" additivity="false">    <level value="ALL" />    <appender-ref ref="SpecialLogFileAppender" /> </logger> <root>    <level value="ALL" />    <appender-ref ref="LogFileAppender" /> </root> 
like image 126
Stefan Egli Avatar answered Sep 30 '22 07:09

Stefan Egli