In the following example, I will create a log that writes logs to the console and the "a. log" file.
[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;
Leleappender ca = new ConsoleAppender ();
Ca. Name = "leleappender1 ";
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;
}
}
}
From TX_OfficeDev's column