override log4net appender threshold - log4net

I would like to know if it's possible to override a log4net appender threshold.
Default threshold level for the appender is ERROR, but for some namespaces i would like to log INFO as well, e.g. program start/stop info messages.
For example override the log level like this.
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
<logger name="My.Main.Program">
<level value="INFO" />
</logger>
</appender>
Thanks in advance.

Related

Configure Log4Net to enable me to debug issues with a Log4Net Splunk appender?

Can someone clue me in to how I might configure Log4net to help with more verbose debugging?
Currently I have a project where I would like to use a Log4Net appender to log to Splunk.
There exists a NuGet package that seems popular: https://github.com/AlanBarber/log4net.Appender.Splunk
Only there is some problem with the URL and I can't figure out how configure either Log4Net or the appender to help me understand what is wrong with my config.
Here is my config:
<configuration>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Console Logger: %date %-5level: %message%newline" />
</layout>
</appender>
<appender name="Splunk" type="log4net.Appender.Splunk.SplunkHttpEventCollector, log4net.Appender.Splunk">
<ServerUrl>https://my_trial_cloud_splunk_server_name.cloud.splunk.com:8088/services/collector</ServerUrl>
<Token>de63d82e-6f5d-4f72-a03c-018d462c30c8</Token>
<RetriesOnError>10</RetriesOnError>
<BatchIntevalMs>100</BatchIntevalMs>
<BatchSizeCount>10</BatchSizeCount>
<SendMode>Parallel</SendMode>
<IgnoreCertificateErrors>True</IgnoreCertificateErrors>
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level [%thread]: %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="Splunk" />
</root>
</log4net>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
I've set the log4net debug to true. Only, even if I have the wrong URL I don't see any log information saying there was trouble with the Splunk endpoint. How do I get more verbose debugging that there is some issue?

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.

File not created using 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

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 events from another assembly in a seperate log4net log

I have the following log4net configuration
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\\CurrentLog.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
Is it possible to log the events from a different assembly in a different file? For example, messages from
Castle.Facilities.NhibernateIntegration
If not that, is there a better way of filtering the events?
I do not want to limit the stream to only errors.
Loggers can be configured by their names. If you're following idiomatic log4net practices, your loggers are named by their full Namespace.TypeName. You can assign appenders to loggers in two ways.
The first is to embed the special appender directly
<logger name="Castle.Facilities.NhibernateIntegration">
<level value="INFO" />
<appender name="CastleNhIntegrationAppender" type="...">
<!-- put the full appender configuration here -->
</appender>
</logger>
Or to define the appender and reference it in the logger
<appender name="CastleNhIntegrationAppender" type="...">
<!-- put the full appender configuration here -->
</appender>
<logger name="Castle.Facilities.NhibernateIntegration">
<level value="INFO" />
<appender-ref ref="CastleNhIntegrationAppender" />
</logger>

Resources