How to remove the word "FATAL" in log.fatal() in log4net. Example [2018-10-26 09:37:14,889] FATAL Log Message - log4net

How to remove the word "FATAL" in log.fatal() in log4net. Example [2018-10-26 09:37:14,889] FATAL Log Message. How to remove FATAL from this by using log4net configuration? or its a default in log.fatal()?

This can be done by editing your configuration and removing [%level] field from the layout pattern.
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date]-[%thread]-**[%level]**-[%logger] - %message%newline" />
</layout>
</appender>

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?

override log4net appender threshold

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.

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

log4net, can't get it working with .net 4.0 mvc app

Trying to get log4net setup for .net 4.0 asp.net mvc2 app. I have bare minimum configuration but nothing is getting logged. Am I missing something?
referencing log4net 1.2.10 released version dll
added in sections
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
added section
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:\\example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
trying to log message with log4net.LogManager.GetLogger("global").Error("test error");
But there is nothing getting logged and no error. Tried lot of config variations like only console, only rollingfile, to file appender only. but nothing works. And yes it's .net 4.0 full project not client profile.
What could be wrong in here?
Did you start log4net?
protected void Application_Start()
{
XmlConfigurator.Configure();
...
}

What is the preferred way to log the conversionPattern value used by the log4net?

My question may seem a little strange - I wish to log the conversionPattern used to format the messages logged with log4net when certain appenders are used.
For instance, if my log4net section looks like this:
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="MainAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="${NCLOGS}\NC.Entities.Test${CI_TAG}.%processid.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<MaximumFileSize value="10MB" />
<StaticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="MainAppender" />
</root>
</log4net>
Then I would like to see %date [%thread] %-5level %logger - %message%newline in the log file.
Rationale I am using Log4View to view our logs and it demands the conversion pattern in order to be able to format the messages. I find it convenient if the conversion pattern is found in the log itself, this spares me the need to look for the respective config file and copy it from there.
Right now I am doing using some XPATH magic, but I am wondering if there is a better solution, probably using some log4net API.
Thanks.
One way to do it would be to add the pattern as a property when you start up the application. You could read the pattern from the config file or there might be a way to get the pattern for each of the appenders programatically, in which case you can add one property for each appender and then have each pattern for each appender log the property.
Something like this to add the property:
log4net.GlobalContext.Properties["PatternForAppender"]= getLoggerPattern();
And something like this to include it in the log:
<conversionPattern value="%logger (%property{PatternForAppender}) [%level] - %message%newline" />

Resources