File not created using Log4Net - log4net

I do not know why my log file is not created . Here is my config entries for Log4Net
<log4Net>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log.txt"/>
<param name="appendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newlineExtra%newline%exception"/>
</layout>
</appender>
<logger name="Log4NetTest.OtherClass">
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
Then at my application startup I take care to run the follwoing line :
log4net.Config.XmlConfigurator.Configure();
And having the following at the declare section of each of my classes :
//use for logging information
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Then when using
log.Fatal("Fatal : Startup start")
it should create a log entry in the outpout directory of my project ! But not file exist
What did I do wrong in here ?
regards
serge

In fact what I do wrong is that I get a section definition as log4*N*et and in the section itself it was define as .
This prevent logging information
regards
serge

Related

How to define different logger levels per appenders

I'm trying to define 2 independent appenders to log into to 2 files. I define
DEBUG level for the "DebugAppender" and then for the "RelevantAppender" I define different levels for the "Security" and "ServerStats" loggers.
The thing is that these loggers definitions are overwriting my "DebugAppender" levels and now it's not in DEBUG mode (all the loggers).
How can make it work as expected?
<log4net>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.debug.log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %thread %property{TransactionID} %property{ClientMachine} %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RelevantAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.relevant.log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %property{ClientMachine} %-5level %logger - %message%newline" />
</layout>`enter code here`
</appender>
<logger name="Security">
<level value="ERROR" />
<appender-ref ref="RelevantAppender" />
</logger>
<logger name="ServerStats">
<level value="INFO" />
<appender-ref ref="RelevantAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
You're problem is, that the definition you make in Logger outweights all following levels. So here comes my solution:
<log4net>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.debug.log.txt" />
<!--...-->
</appender>
<appender name="Security_RelevantAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ERROR" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="plastic.relevant.log.txt" />
<!--...-->
</appender>
<appender name="Serverstat_RelevantAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--...-->
</appender>
<logger name="Security">
<appender-ref ref="Security_RelevantAppender" />
</logger>
<logger name="ServerStats">
<appender-ref ref="Serverstat_RelevantAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
</log4net>
Define 3 different appenders. The DebugAppender's level is defined by <root>. The two relevant appenders write both to the same file with different log-levels (thresholds). It is important, that you include the locking model <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />. Utherwise the first appender you call will lock the file and the second one can not write in it (Can Log4net have multiple appenders write to the same file?). In the <logger> you just define the two different appenders and no loglevel. This way it will take the level from the appender settings.
When you do it like this all events of the type debug+ from any logger will be written to plastic.debug.log.txt. To plastic.relevant.log.txt only events error+ from security-logger and info+ from serverstat-logger are written.
Hope this helps

Prevent Log4Net from writing to all log files

I've got the following configuration using log4net. The problem is that the logger in my C# code is now logging the errors into the two log files. I've got two different service classes in the same windows service. I initialize the logger in one service using this line:
private static readonly ILog _logger = LogManager.GetLogger(typeof(EmployeeImportService));
But when this service runs and logs, it's writing to both log files.
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\Sharp\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="OvertimeLogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\YTD\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
</log4net>
How do I configure it so that each service writes to it's own log file? I read that the logger tag in the configuration has an additivity attribute that solves this problem but I have no logger element in my configuration.
You can have two separate loggers, and define which appenders each logger will use in the configuration: you do have to declare additivity=false or else the loggers inherit the appenders from the root logger.
So, with made-up names:
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
<logger name="Company.Project.EmployeeImportService" additivity="false">
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="Company.Project.EmployeeOvertimeService" additivity="false">
<appender-ref ref="OvertimeLogFileAppender" />
</logger>
Then in your service classes you get the approppriate logger:
// General log - EmployeeImportService
ILog logger = LogManager.GetLogger(typeof(EmployeeImportService));
// Log overtime - EmployeeOvertimeService
ILog logger = LogManager.GetLogger(typeof(EmployeeOvertimeService));
Oups yes I did a mistake. But it quiet the same. You have to create filter in each appender node
<filter type="log4net.Filter.LoggerMatchFilter">
<!-- allows this sub-namespace to be logged... -->
<loggerToMatch value="EmployeeImportService" />
</filter>

log4Net - Loggers between classes - Releasing a File

I am new to log4net. Here is my app.config. What is happening is at a point after going through a switch statement, my logger "X" stops writing to my log file. I tried x.Logger.Repository.Shutdown();, and all that does is stop my "Y" from even logging. My suspicion is that the file is in use, and can't be written to (the next logging statement does begin in a different class. Again, I'm new. Basically, it writes about 10 lines, goes to another class with a logger of the same name, works in that class for a few more lines, and then when it goes back to the original class, it stops logging. I'm a little lost. P.S. I have the correct section name, and I am calling my log as such:
private static readonly ILog eventLog = LogManager.GetLogger("EventLog");
<appender type="log4net.Appender.FileAppender" name="event">
<file value="C:X.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender type="log4net.Appender.FileAppender" name="cLog">
<file value="Y.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="EventLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="event" />
</logger>
<logger name="crLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="cLog" />
</logger>
As it turns out, when you call x.Logger.Repository.Shutdown(); it kills any logs that you might want to write to at that point in time.
I will either move x.Logger.Repository.Shutdown(), or find a different method for releasing my files so the last portion of my app can use (email) the file.

Log4net: no logs written when application running in Win8 without "as administrator"

Our application uses log4net to write some logs to the local disk with RollingFileAppender. It runs well in local developing machines until we test it with remote desktop connection in virtual machines.
After install it in Win8, we have to run it as administrator to let it write logs. Even if we login the remote win8 with a local administrator account of the win8.
Things are different in win7. The application always writes logs even if we login the remote win7 with a domain user. Maybe you are interested in the config file, let me put it here:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\mylogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="RollingFileAppender"/>
</root>
Your user does not have permissions to write to c:\mylogfile.txt. Make a directory c:\temp and try to log to c:\temp\mylogfile.txt
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\temp\mylogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="RollingFileAppender"/>
</root>

Log4net - Create new log file each time application is started

Can we create separate log files each time when the application is run? If i run my application 2 times, I should get 2 separate log files, hopefully the file names can be appended with the created dateTime
eg:
log_0830 - when application is run on 8:30 am
log_2130 - when application is run on 9:30 pm
Put that into your app.config:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="log_"/>
<rollingStyle value="Date"/>
<datePattern value="HHmm.\tx\t"/>
<staticLogFileName value="false"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="file"/>
</root>
</log4net>
That configuration would produce filenames:
log_0830.txt - when application is run on 8:30 am
log_2130.txt - when application is run on 9:30 pm
I believe you could do this in the configuration like so:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-%d [%t].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>

Resources