Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 09 January 2015
( Log4Net )

http://www.codeproject.com/Articles/140911/log-net-Tutorial

OFF - nothing gets logged (cannot be called)
FATAL
ERROR
WARN
INFO
DEBUG
ALL - everything gets logged (cannot be called)

<root> <level value="INFO"/> <appender-ref ref="FileAppender"/> <appender-ref ref="ConsoleAppender" /> </root>

Minimum level to log here is INFO.  So DEBUG would be ignored.
  • What is good to log?
  • Where to log – probably need the db!
  • How to test.. inject in the logger?
  • RollingFileAppender or just a very large text file?

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
     <file value="log-file.txt" />
     <appendToFile value="true" />
     <layout type="log4net.Layout.PatternLayout">
           <!-- logger{1} gets rid of the namespace—>
           <conversionPattern value="%date{dd-MM-yy HH:mm:ss} %-5level %logger{1} - %message%newline" />
     </layout>
   </appender>

testing: MemoryAppender

http://stackoverflow.com/questions/5608617/log4net-memoryappender-not-working

| | # 
# Wednesday, 13 March 2013
# Tuesday, 24 August 2010
( Log4Net )

http://logging.apache.org/log4net/index.html

Logging to the console in a console app: (thanks to http://www.beefycode.com/?tag=/log4net for the great examples)

static void Main(string[] args)
{
log4net.Config.BasicConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

log.Debug("Hello World!");
log.Info("I'm a simple log4net tutorial.");
log.Warn("... better be careful ...");
log.Error("ruh-roh: an error occurred");
log.Fatal("OMG we're dooooooomed!");

Console.ReadLine();
}

Using app.config

image

<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>

<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

notice level is set to INFO only.

Appenders

An object that persists the log messages somewhere.

eg AdoNetAppender, ConsoleAppender, ColoredConsoleAppender, EventLogAppender, FileAppender (don’t use), RollingFileAppender, SmtpAppender

image

Layout

All the different options:

http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%line %date %-5level %logger - %message%newline" />
</layout>
image

Real Life

class Program
{
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
Log.Info("this is an info message");
MyClass.WriteLog();

Console.ReadLine();
}
}

class MyClass
{
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public static void WriteLog()
{
Log.Info("this is an info message");
}
}

image

Shows us which class called the logger.

Web Apps

Putting this in AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator()]

Connection Strings and AdoNetAppender

bufferSize value=”1” – otherwise it won’t write each one seperatly

This proved hard

http://weblogs.asp.net/drnetjes/archive/2005/02/16/374780.aspx

doing a search and replace here didn’t work

Nor did extending the provider

http://stackoverflow.com/questions/2441359/can-you-pull-the-connectionstring-for-a-log4net-adonetappender-from-elsewhere-in

** this could be better: http://blog.dynamicprogrammer.com/CommentView,guid,357a3fbe-59e7-4dd4-846a-89083e903532.aspx

Recommended Practice in Code

http://www.beefycode.com/post/Log4Net-Recommended-Practices-pt-1-Your-Code.aspx

Whenever you catch an exception, log it.

Internal Log4Net Logging

When trying to figure out why somethings aren’t working (eg connection strings)

In web.config:

<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="c:\\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>

| | #