在下面这个示例中,我将创建一个向控制台和“a.log”文件中写入日志的log
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Appender;
using log4net.Layout;
using log4net.Repository.Hierarchy;
namespace MyLog4Net
{
public class MyLog4Net
{
public static ILog GetMyLog(dynamic LogName)
{
ILog log = null;
if (LogName is string)
{
string name = (string)LogName;
log = log4net.LogManager.GetLogger(name);
}
else if (LogName is Type)
{
Type t = (Type)LogName;
log = log4net.LogManager.GetLogger(t);
}
else
{
throw new Exception(“Parameter must be a string or Type value!”);
}
FileAppender fa = new FileAppender();
fa.Name = “FileAppender1”;
fa.File = “a.log”;
fa.AppendToFile = true;
ConsoleAppender ca = new ConsoleAppender();
ca.Name = “ConsoleAppender1”;
PatternLayout pl = new PatternLayout();
pl.ConversionPattern = “[%d] - %p : %c => %m%n”;
pl.ActivateOptions();
fa.Layout = pl;
fa.ActivateOptions();
ca.Layout = pl;
ca.ActivateOptions();
Hierarchy h = (Hierarchy)LogManager.GetRepository();
h.Root.Level = h.LevelMap[“DEBUG”];
h.Root.AddAppender(fa);
h.Root.AddAppender(ca);
h.Configured = true;
return log;
}
}
}
发表回复