What's wrong with my log4net config file - log4net

I have a config file with two appenders, one file appender and one database appender. I want to log everything to the file appender, and only log exceptions to the database appender. When setting up both appenders in the section it logs fine but all log events are sent to both appenders, which is not what i want.
I changed the configuration but with this current configuration, exceptions get logged to the database, and nothing is getting written to the file appender. Can anyone tell me why I am not getting anything written to the file appender?
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Log4net\Workflow\TestLog.txt" />
<threshold value="All" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p {%logger} %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<!-- Removed to keep this snippet simple-->
</appender>
<root>
<level value="Error" />
<appender-ref ref="AdoNetAppender" />
</root>
<logger name="AllLogs">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>

What you have here is the following:
all logs events with the level Error or more will go to the AdoNetAppender
all logs events originating from a logger whose name is based on AllLogs will go to the RollingLogFileAppender
From what I understand you want all logs to default to the file, and only error ones to go also to the database. Then simply add both appenders to your root logger so that both get all events, and add filters to only let filters you're interested in pass through: a level range filter on your database appender would work
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- rest of config snipped to save space -->
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<!-- rest of config snipped to save space -->
</appender>
<root>
<appender-ref ref="AdoNetAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Of course if you don't want any duplicate just filter errors and above in the rolling file appender

Related

ASP.NET Boilerplate adding an additional log file with Log4Net and Castle Logging

I've been trying to find documentation and guidance on how to use a separate log file in ASP.NET Boilerplate MVC.
It's straight forward to inject ILogger and push messages into the default Log.txt file, however I need a separate log file to record a lot of batch job messages and keep things tidy.
I can't find any methods to use another log from the Log4Net.config file. Can anyone advise on the correct code / configuration?
So I found the answer by chance in looking at the Hangfire documentation, where it was shown you can configure Hangfire messages to be sent to another log file by getting Log4Net to listen for the Hangfire name and route based on that.
With the following additional configuration in Log4Net.config I now have Log messages created normally within MyClass sent to the MyClass.txt log file:
<appender name="RollingFileAppender_MyClass" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/MyClass.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="32" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<logger additivity="false" name="MyNamespace.MyClass">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender_MyClass" />
</logger>
And for completeness, it's called in the standard way documented within the AspNet Boilerplate documentation:
https://aspnetboilerplate.com/Pages/Documents/Logging
Way-1:
In this sample, the default log file is App_Data/Logs/Logs.txt.
But when I use the logger in the namespace MySite.Web.Controllers.PaymentController, the logs will be written in App_Data/Logs/PaymentLogs.txt
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender_PaymentController" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/PaymentLogs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="32" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="DEBUG" />
</root>
<logger name="NHibernate">
<level value="WARN" />
</logger>
<logger name="MySite.Web.Controllers.PaymentController" additivity="false">
<appender-ref ref="RollingFileAppender_PaymentController" />
<level value="DEBUG" />
</logger>
</log4net>
Way-2:
You can get a specific logger by calling LogManager.GetLogger() with a appender name.
private static readonly ILog paymentLogger = LogManager.GetLogger("MySite.Web.Controllers.PaymentController");

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

Different levels of logging with log4net

I must log some methods. In one method I must log everything. In all others method I've just log exception...
I have done this configuration:
<log4net>
<appender name="OneMethodFileAppender" type="log4net.Appender.RollingFileAppender">
...
<filter type="log4net.Filter.PropertyFilter">
<key value="realMethod" />
<stringToMatch value="xxx" />
<acceptOnMatch value="true" />
</filter>
</appender>
<appender name="WebsiteFileAppender" type="log4net.Appender.RollingFileAppender">
...
<filter type="log4net.Filter.PropertyFilter">
<key value="realMethod" />
<stringToMatch value="xxx" />
<acceptOnMatch value="false" />
</filter>
</appender>
<root>
<appender-ref ref="WebsiteFileAppender">
<threshold value="ERROR" />
</appender-ref>
<appender-ref ref="OneMethodFileAppender">
<threshold value="INFO" />
</appender-ref>
</root>
</log4net>
So, when I match the property realMethod I should use the appender OneMethodFileAppender that log at the INFO level; in all other cases i should use the appender WebsiteFileAppender that log at ERROR level.
I have a different output.
1. When i match the method xxx everything works correctly. I mean that a log at INFO level in the correct log.
2. But when I do not match the method xxx, I log in both files and both at INFO level.
I would suggest to use the logger element to configure your logging:
<log4net>
<appender name="OneMethodFileAppender" type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="WebsiteFileAppender" type="log4net.Appender.RollingFileAppender">
...
</appender>
<root>
<appender-ref ref="WebsiteFileAppender">
<threshold value="ERROR" />
</appender-ref>
</root>
<logger name="LoggerName1">
<level value="INFO" />
<appender-ref ref="WebsiteFileAppender" />
</logger>
<logger name="LoggerName2">
<level value="INFO" />
<appender-ref ref="OneMethodFileAppender" />
</logger>
</log4net>
Both methods should have there own logger:
logger = LogManager.GetLogger("logger1");

EventLogAppender - How to stop errors being logged into application Event log

I am using log4net – EventLogAppender to log the application errors to a custom log. This happens, but the errors are also logged in Application log. Since I have my own custom event log say myLog, is it possible to stop the errors being logged into default log Application?
<log4net debug="true">
<appender name="sendAlertAppender" type="sendAlertAppender.MultiThresholdNotifyingAppender,sendAlertAppender">
<LevelThreshold value="ERROR"/>
<appender-ref ref="EventLogAppender"/>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="LogName" value="xxxWebLogs" />
<param name="ApplicationName" value="xxx" />
<eventId value="5" />
<eventCategory value="10" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<priority value="DEBUG" />
<appender-ref ref="sendAlertAppender"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
I'm still not sure what your issue is, but looking at the configuration I believe you need to change the LogName setting:
<param name="LogName" value="PortalWebLogs" />
If this doesn't work, it could be a permissions issue. See here for more information:
log4Net EventlogAppender does not work for Asp.Net 2.0 WebSite?
I haven't done much with using the event log appender, so i'm not really sure. here's another question talking about it and in the answer, it shows using a logName element with a value attribute instead of a param element. might be worth trying that.
Configuring a custom event log for log4net

log information into different log files

I have the following configurations in my web.config file, but how can I log the information into data.txt and general.txt separately in C#?
Could anyone provide some sample code for me?
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
<file value="App_Data/Logs/general.txt" />
<appendToFile value="true" />
<maximumFileSize value="2MB" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="DataLog" type="log4net.Appender.RollingFileAppender">
<file value="App_Data/Logs/data.txt" />
<appendToFile value="true" />
<maximumFileSize value="2MB" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
In your app, ideally, you should log the same way for both general and data, just using different loggers. In configuration you have a few options how to "route" the incoming log messages to different appenders.
The first is the loggers themselves. Depending on how your app is organized in terms of classes and namespaces, you organize your loggers in a hierarchical manner according to your namespace hierarchy. Then you can have different branches in the hierarchy going to different appenders. Read up on the Level Inheritance section here.
In addition to logger hierarchies, you can use any name as logger names. So for your general sections of your app, you could use a logger named "general".
Next thing you can use to control log message flow is using filters. You can filter messages on logger name, level, properties etc. or you can implement your own filters.
Here's a sample to show how you could route general and data to your appenders using the logger element and a filter. We route the root element (which receives all log messages) to the data appender. At the data appender we filter out all messages coming from the general logger.
Additionally, we route the general logger to the general appender.
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="DataLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="general" />
<acceptOnMatch value="false" />
</filter>
...
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="DataLog" />
</root>
<logger name="general">
<level value="WARN" />
<appender-ref ref="GeneralLog" />
</logger>

Resources