How do I make Log4net only log Info level logs? Is that even possible? Can you only set a threshold?
This is what I have, and it logs Info and above as I would expect. Is there anything i can do to make it only log info?
<logger name="BrokerCollection.Model.XmlDocumentCreationTask">
<appender-ref ref="SubmissionAppender"/>
<level value="Info" />
</logger>
Within the definition of the appender, I believe you can do something like this:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
...
</appender>
Use threshold.
For example:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="WARN"/>
<param name="File" value="File.log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Size" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="1024KB" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Server startup]
" />
<param name="Footer" value="[Server shutdown]
" />
<param name="ConversionPattern" value="%d %m%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread]- %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<threshold value="INFO"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%thread] %m%n" />
</layout>
</appender>
In this example all INFO and above are sent to Console, all WARN are sent to file and ERRORs are sent to the Event-Log.
Yes. It is done with a filter on the appender.
Here is the appender configuration I normally use, limited to only INFO level.
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${HOMEDRIVE}\\PI.Logging\\PI.ECSignage.${COMPUTERNAME}.log" />
<appendToFile value="true" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="5MB" />
<rollingStyle value="Size" /> <!--A maximum number of backup files when rolling on date/time boundaries is not supported. -->
<staticLogFileName value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.ffff} [%2thread] %-5level %20.20type{1}.%-25method at %-4line| (%-30.30logger) %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="INFO" />
</filter>
</appender>
If you would like to perform it dynamically try this:
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;
using NUnit.Framework;
namespace ExampleConsoleApplication
{
enum DebugLevel : int
{
Fatal_Msgs = 0 ,
Fatal_Error_Msgs = 1 ,
Fatal_Error_Warn_Msgs = 2 ,
Fatal_Error_Warn_Info_Msgs = 3 ,
Fatal_Error_Warn_Info_Debug_Msgs = 4
}
class TestClass
{
private static readonly ILog logger = LogManager.GetLogger(typeof(TestClass));
static void Main ( string[] args )
{
TestClass objTestClass = new TestClass ();
Console.WriteLine ( " START " );
int shouldLog = 4; //CHANGE THIS FROM 0 TO 4 integer to check the functionality of the example
//0 -- prints only FATAL messages
//1 -- prints FATAL and ERROR messages
//2 -- prints FATAL , ERROR and WARN messages
//3 -- prints FATAL , ERROR , WARN and INFO messages
//4 -- prints FATAL , ERROR , WARN , INFO and DEBUG messages
string srtLogLevel = String.Empty;
switch (shouldLog)
{
case (int)DebugLevel.Fatal_Msgs :
srtLogLevel = "FATAL";
break;
case (int)DebugLevel.Fatal_Error_Msgs:
srtLogLevel = "ERROR";
break;
case (int)DebugLevel.Fatal_Error_Warn_Msgs :
srtLogLevel = "WARN";
break;
case (int)DebugLevel.Fatal_Error_Warn_Info_Msgs :
srtLogLevel = "INFO";
break;
case (int)DebugLevel.Fatal_Error_Warn_Info_Debug_Msgs :
srtLogLevel = "DEBUG" ;
break ;
default:
srtLogLevel = "FATAL";
break;
}
objTestClass.SetLogingLevel ( srtLogLevel );
objTestClass.LogSomething ();
Console.WriteLine ( " END HIT A KEY TO EXIT " );
Console.ReadLine ();
} //eof method
/// <summary>
/// Activates debug level
/// </summary>
/// <sourceurl>http://geekswithblogs.net/rakker/archive/2007/08/22/114900.aspx</sourceurl>
private void SetLogingLevel ( string strLogLevel )
{
string strChecker = "WARN_INFO_DEBUG_ERROR_FATAL" ;
if (String.IsNullOrEmpty ( strLogLevel ) == true || strChecker.Contains ( strLogLevel ) == false)
throw new Exception ( " The strLogLevel should be set to WARN , INFO , DEBUG ," );
log4net.Repository.ILoggerRepository[] repositories = log4net.LogManager.GetAllRepositories ();
//Configure all loggers to be at the debug level.
foreach (log4net.Repository.ILoggerRepository repository in repositories)
{
repository.Threshold = repository.LevelMap[ strLogLevel ];
log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)repository;
log4net.Core.ILogger[] loggers = hier.GetCurrentLoggers ();
foreach (log4net.Core.ILogger logger in loggers)
{
( (log4net.Repository.Hierarchy.Logger)logger ).Level = hier.LevelMap[ strLogLevel ];
}
}
//Configure the root logger.
log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository ();
log4net.Repository.Hierarchy.Logger rootLogger = h.Root;
rootLogger.Level = h.LevelMap[ strLogLevel ];
}
private void LogSomething ()
{
#region LoggerUsage
DOMConfigurator.Configure (); //tis configures the logger
logger.Debug ( "Here is a debug log." );
logger.Info ( "... and an Info log." );
logger.Warn ( "... and a warning." );
logger.Error ( "... and an error." );
logger.Fatal ( "... and a fatal error." );
#endregion LoggerUsage
}
} //eof class
} //eof namespace
The app config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="LogTest2.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header] \r\n" />
<param name="Footer" value="[Footer] \r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.2.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=ysg;initial catalog=DBGA_DEV;integrated security=true;persist security info=True;" />
<commandText value="INSERT INTO [DBGA_DEV].[ga].[tb_Data_Log] ([Date],[Thread],[Level],[Logger],[Message]) VALUES (#log_date, #thread, #log_level, #logger, #message)" />
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</parameter>
<parameter>
<parameterName value="#log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%messag2e" />
</parameter>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AdoNetAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
</configuration>
The references in the csproj file:
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Log4Net\log4net-1.2.10\bin\net\2.0\release\log4net.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
you can use log4net.Filter.LevelMatchFilter.
other options can be found at
log4net tutorial - filters
in ur appender section add
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="Info" />
<acceptOnMatch value="true" />
</filter>
the accept on match default is true so u can leave it out but if u set it to false u can filter out log4net filters
Related
I'd like to have four log files per day having date as part of the file name.
Something like:
MYAPP-20190108-4.log
MYAPP-20190108-3.log
MYAPP-20190108-2.log
MYAPP-20190108-1.log
MYAPP-20190107-4.log
MYAPP-20190107-3.log
MYAPP-20190107-2.log
MYAPP-20190107-1.log
I know I can leverage date pattern but then I end up with 24 files...
<Logging Message2FilePath="C:\Messages">
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="RollingStyle" value="Composite" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="false" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="10MB" />
<file type="log4net.Util.PatternString" value="C:\Logs\MYAPP-" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yy HH:mm:ss,fff} [%-5p][%3t]%m%n" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<datePattern value="yyyyMMdd-HH'.log'" />
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yy HH:mm:ss,fff} [%-5p][%3t]%m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</Logging>
Key settings:
<param name="RollingStyle" value="Composite" />
<file type="log4net.Util.PatternString" value="C:\Logs\MYAPP-" />
<datePattern value="yyyyMMdd-HH'.log'" />
Resulting output:
MYAPP-20190107-13.log
MYAPP-20190107-12.log
MYAPP-20190107-11.log
MYAPP-20190107-19.log
MYAPP-20190107-09.log
Would that be possible ?
Thanks for the pointer #ChetanRanpariya.
I had to override RollingFileAppender, here is a solution:
class RollingOverDaypartFileAppender : RollingFileAppender
{
private DateTime nextRolloverDate;
public RollingOverDaypartFileAppender(){}
protected override void AdjustFileBeforeAppend()
{
if (DateTime.Now.Hour % 6 != 0) return;
base.AdjustFileBeforeAppend();
}
}
I use Castle.Windsor and Log4Net for initialize my Logger.
My log4net.config:
<?xml version="1.0"?>
<configuration>
<log4net>
<appender name="AllMessagesFile" type="log4net.Appender.RollingFileAppender,log4net">
<file value="Logs/Log.log" />
<level value="ALL" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maximumFileSize value="100000KB" />
<maxSizeRollBackups value="-1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %identity [%t] %-5p %c{1}.%M %m%n" />
<IgnoresException value="False" />
</layout>
</appender>
<appender name="ErrorsOnlyFile" type="log4net.Appender.RollingFileAppender,log4net">
<file value="Logs/Error.log" />
<level value="ERROR" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maximumFileSize value="100000KB" />
<maxSizeRollBackups value="-1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %identity [%t] %-5p %c{1}.%M %m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="AllMessagesFile" />
<appender-ref ref="ErrorsOnlyFile" />
</root>
</log4net>
</configuration>
My AssamblyInfo.cs have foolow row:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
In constructor of class I got object via Winsdor:
public class ACIStart
{
private ILogger Logger { get; set; }
public ACIStart(ILogger logger)
{
Logger = logger;
}
I don't have any exceptions, I just got a logger with all properties (IsDebbugEnabled, IsErrorEnabled ... ) as false.
My log no write anything to log file.
What could be the problem?
It could be that you need to add the loglevel in you root node:
<root>
<level value="DEBUG" />
...
Trying to understand the log4net filtering.
After reading the docs some aspects are still elusive to me.
For example I have this config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start" />
<footer value="Service Logging -- End" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="WindowsService1.exe" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (“namespace.class”)-->
<logger name="WindowsService1.MyMonitor">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
My expectations were:
The RollingFileAppender has a range of [INFO:FATAL] The Root has a
Level of DEBUG so it would let DEBUG and "greater" pass. Which
means all of RollingFileAppender's range would go through.
The LogFileAppender has no range specified so it would use Root's of DEBUG or greater.
And the EventLogAppender would only send event messages of WARN or greater to the Windows Event System - and - they would only
be messages from the class "WindowsService1.MyMonitor".
However it appears that all the messaging gets filtered to the WARN level!
Including my rolling log files and the log file "log-file.txt"
This seems like it should be simple - don't know why I'm so confused by it.
Hope someone can enlighten me.
Please feel free to use crayons and pictures because I think I need them.
Thanks.
UPDATE
In my Program.cs I have:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
And in each class where logging is done:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
UPDATE 2
At a different computer now. Had to recreate everything and used log4net debugging to assist.
But still can't get the desired output.
Basically want the WARN and above messages to go to one file and the Event logger. And those messages would be a subset of all the INFO and above messages.
The INFO and above messages - superset - would go into the rolling appender and overwrite themselves eventually. But the warnings and errors would always be maintained.
So here is a rework of all the files for a simple service. If I set WARN on the appender-ref ref=EventLogAppender then all the INFOs are suppressed.
Program.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
// Ref: http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/
//
// use 'sc' to install/delete the service
// run 'sc' as administrator
// sc create "MySillyService" binpath= "C:\whatever\something.exe"
// sc delete "MySillyService"
namespace SimpleService1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
Service1.cs
namespace SimpleService1
{
public partial class Service1 : ServiceBase
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private Timer timer1;
private int counter = 0;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
timer1.Interval = 4000;
timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
timer1.Enabled = true;
log.Info("my service is started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
counter++;
if (counter%2 == 0)
{
log.Info(String.Format("Counter({0:D}) is now even", counter));
}
else
{
log.Error(String.Format("Counter({0:D}) is now even", counter));
}
}
protected override void OnStop()
{
timer1.Enabled = false;
log.Info("my service was stopped");
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<!--
Ref: http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Service1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (�namespace.class�)-->
<logger name="SimpleService1.Service1">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
Thanks for your help
Finally figured out the nuances to get this working as desired. Here's the config for anyone interested.
<?xml version="1.0" encoding="utf-8" ?>
<!--
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<!-- Put whatever name you want to display in the event log
for 'applicationName value=XX'
-->
<applicationName value="ServiceSkippy" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- put namespace of service application in following name -->
<logger name="ServiceWithLog4Net">
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
-Skippy
I am trying to add a new rolling file appender to an existing log4net configuration. I have been looking at questions on S.O. and looking at the documentation however I can't seem to see where my issue is.
The problem is that it creates the file but never writes to it. All I can come up with is that somehow I have misunderstood how the hierarchies work? If I attached a debugger I can see the line get written to the console but even when I did a test deploy the log file document never has anything written to it. There are other questions about this however they all have issue with levelMin or something like that. I have tried setting my config to ALL, didn't help either.
Here is complete config so you can see what I am talking about.
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
<root>
<level value="ALL"/>
<appender-ref ref="CompassServiceTraceAppender" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="DbAppender" />
</root>
<logger name="Compass.Service.Implementation.EdiService">
<level value="ALL"/>
<appender-ref ref="EventProcessingFailureLogFileAppender" />
</logger>
<logger name="Compass.Service.Implementation.PhoneService">
<level value="ALL"/>
<appender-ref ref="LivevoxResultsReprocessorAppender"/>
</logger>
<logger name="DocumentImportLogger">
<level value="ALL"/>
<appender-ref ref="DocumentLogFileAppender" />
</logger>
<logger name="NHibernate">
<level value="ERROR"/>
<appender-ref ref="CompassServiceTraceAppender" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="DbAppender" />
</logger>
<appender name="DbAppender" type="log4net.Appender.AdoNetAppender">
<threshold value="WARN" />
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=.;initial catalog=logDb;integrated security=True;persist security info=True;" />
<commandText value="INSERT INTO ErrorLog ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Environment],[Application],[User],[ObjectName],[ObjectId],[SupportId]) VALUES (#log_date, #thread, #log_level, #logger, #message, #exception, 'localhost','Compass BB', #user, #objectName, #objectId, #supportId)" />
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</parameter>
<parameter>
<parameterName value="#log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</parameter>
<parameter>
<parameterName value="#exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%exception" />
</parameter>
<parameter>
<parameterName value="#user" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{CurrentUser}" />
</parameter>
<parameter>
<parameterName value="#objectName" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{ObjectName}" />
</parameter>
<parameter>
<parameterName value="#objectId" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{ObjectId}" />
</parameter>
<parameter>
<parameterName value="#supportId" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%property{SupportId}" />
</parameter>
</appender>
<appender name="CompassServiceTraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<threshold value="WARN" />
<param name="ApplicationName" value="Compass Best Benefits Service Host" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger %newline User: %property{CurrentUser} Object: %property{ObjectName} Id: %property{ObjectId} %newline %message %newline %exception"/>
</layout>
</appender>
<appender name="EventProcessingFailureLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\EventProcessingFailure_log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message %newline %exception" />
</layout>
</appender>
<appender name="LivevoxResultsReprocessorAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\Livevoxreprocessor.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %newline" />
</layout>
</appender>
<!--DocumentLogFileAppender-->
<appender name="DocumentLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ALL" />
<param name="File" value="c:\logs\DocumentLog.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<param name="maxSizeRollBackups" value="10" />
<param name="maximumFileSize" value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %newline" />
</layout>
</appender>
</log4net>
Here is the code example.
private static readonly HALogger Logger = new HALogger("DocumentImportLogger");
//which calls
public HALogger(string loggerName)
{
_logger = LogManager.GetLogger(loggerName);
}
//Then to log.
Logger.Info(string.Format("Checking for new documents - LastDocumentId: {0}", LastDocInfo.LastDocumentId));
Your config file looks fine to me.
The problem is that log4net silently ignores configuration errors by default and just silently fails - which is a sensible design choice for a logging framework but can be frustrating at times.
You can try enabling internal debugging and programmatically checking for configuration errors.
See the log4net faq => the relevant sections are:
"How do I enable log4net internal debugging?"
"How can I evaluate configuration errors at runtime?"
Also, please add a code example of how you obtain a logger and try writing to it.
I have created a sample program and every time I run this program, it is creating a dummy entry in the event log as below. How to avoid without creating a logger?
2014-02-24 18:04:53,557 [8] ERROR Log4NetSample.Program [(null)] - Error message
My Code:
using log4net;
using log4net.Config;
namespace Log4NetSample
{
class Program
{
static void Main(string[] args)
{
Program pg= new Program();
pg.Log4netCode();
}
private void Log4netCode()
{
XmlConfigurator.Configure();
ILog log = LogManager.GetLogger(typeof(Program));
log.Debug("Debug message");
log.Info("Info message");
log.Warn("Warning message");
log.Error("Error message");
log.Fatal("Fatal message");
}
}
}
Config file:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\Log\MyFirstLogger.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="2KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<Eventid value="100"/>
<LogName value="Log4NetSample" />
<applicationName value="Log4NetSample" source="Api" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
You message is the actual to message from the line log.Error. You do not need to log or test you logger right after init the config.