Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Log4net have multiple appenders write to the same file?

I'm using a RollingFileAppender to log some info to a file with a conversionPattern (in the web.config) that looks like this for the header of each log section:

<conversionPattern value="%date - %property{userId} - %property{method}%newline--------------------------------%newline%message%newline%newline"/>

I'd like to log details under this header as bullet points. I'm currently trying to use another RollingFileAppender that logs to the same file with a simple conversionPattern of just a dash, like this:

<conversionPattern value="- %message%newline"/>

But these messages aren't making it into the log file. I'm using Log.Info() for the header and Log.Debug() for the bullet points and filtering each appender on their respective log levels. Is what I'm trying to do possible? Or is there a better way to get header and detail information into a log file from log4net?

like image 503
adam0101 Avatar asked Jun 09 '10 22:06

adam0101


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 are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

What is rolling file Appender in log4net?

"When set to Once the log file will be rolled when the appender is configured. This effectively means that the log file can be rolled once per program execution. So, when you set your RollingFileAppender to "once", then every time you execute your program, a new log file will be created.

Where does log4net write to?

In your case, the log file will be in bin\Debug\netcoreapp3.


2 Answers

Yes you can have two log4net appenders that append (write) to the same log file.

You need to place the following line in each of your Appenders:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 

This will make log4net use a minimal locking model that allows multiple processes to write to the same file.

Here's an example XML that uses two appenders writing to the same log file:

<log4net debug="false"> <appender name="RollingLogFileAppender1" type="log4net.Appender.RollingFileAppender">   <!-- this configures a log for the application messages -->   <file value="TestLog.log" />   <appendToFile value="true" />   <!-- next line uses a minimal locking model that allows multiple processes to write to the same file -->   <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />   <rollingStyle value="Size" />   <maxSizeRollBackups value="10" />   <maximumFileSize value="10MB" />   <staticLogFileName value="true" />   <!-- make the most recent log the highest numbered log -->   <countDirection value="1" />   <layout type="log4net.Layout.PatternLayout">     <conversionPattern value="%-5level %date{MM-dd-yyyy HH:mm:ss.ff} [%property{NDC}] %message%newline [Thread: %thread] %c{1} Method:%method(%file{1}, Line:%line) %newline" />   </layout>   <!-- The following two filters insure only log requests of          version '1' use this Appender --> </appender> <appender name="RollingLogFileAppender2" type="log4net.Appender.RollingFileAppender">   <file value="TestLog.log" />   <appendToFile value="true" />   <!-- next line uses a minimal locking model that allows multiple processes to write to the same file -->   <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />   <rollingStyle value="Size" />   <maxSizeRollBackups value="10" />   <maximumFileSize value="10MB" />   <staticLogFileName value="true" />   <!-- make the most recent log the highest numbered log -->   <countDirection value="1" />   <layout type="log4net.Layout.PatternLayout">     <conversionPattern value="%-5level %date{MM-dd-yyyy HH:mm:ss.ff} [%property{NDC}] [Thread: %thread] %c{1} Method:%method(%file{1}, Line:%line) %newline%message" />   </layout> </appender> <root>   <level value="DEBUG" />   <appender-ref ref="RollingLogFileAppender1" />   <appender-ref ref="RollingLogFileAppender2" /> </root> 

This can be found in the Apache documentation here: Apache Log4Net Docs Just search on this page for 'same file'.

Hope this helps.

like image 185
Steve Avatar answered Sep 21 '22 13:09

Steve


You could realize if there is any problem with log4net checking the output window on visual studio. The library log errors there, very useful to detect configuration mistakes.

like image 40
Claudio Redi Avatar answered Sep 20 '22 13:09

Claudio Redi