Friday, September 11, 2009

Log4Net - load config from file and demo basic log


public class Program
{
private static readonly log4net.ILog alog = LogManager.GetLogger("AlgoLogger");

public static void Main(string[] args)
{
FileInfo fInfo = new FileInfo(@"C:\this\that\log4net.config");
log4net.Config.XmlConfigurator.Configure(fInfo);

Klass klass = new Klass();
klass.LogStuff();
}
}

public class Klass
{
private static readonly log4net.ILog alog = LogManager.GetLogger("AlgoLogger");

public void LogStuff()
{
string logStr = "log info";
alog.Debug(logStr);
}
}


log4net.config XML file

[?xml version="1.0" encoding="utf-8" ?]
[log4net]
[appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" ]
[layout type="log4net.Layout.PatternLayout"]
[conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /]
[/layout]
[/appender]

[appender name="RollingFile" type="log4net.Appender.RollingFileAppender"]
[file value="log-file.txt" /]
[appendToFile value="true" /]
[maximumFileSize value="1024KB" /]
[maxSizeRollBackups value="10" /]
[rollingStyle value="Size" /]
[layout type="log4net.Layout.PatternLayout"]
[conversionPattern value="%date %level %thread %logger - %message%newline" /]
[/layout]
[/appender]

[!--
[root]
[level value="ALL" /]
[appender-ref ref="RollingFile" /]
[/root]
--]

[logger name="AlgoLogger"]
[level value="ALL" /]
[appender-ref ref="AlgoAppender" /]
[/logger]

[/log4net]




No comments:

Post a Comment

comment: