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

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?

Related

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.

What's wrong with my log4net config file

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

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>

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();
...
}

Resources