I recently had to knock together a very simple logging class for a simple application I was creating to monitor some website activity. My favourite logging application by far is log4net!!
This is a simple static class which can be called anywhere else in the solution. In effect it is just a wrapper. The way that log4net is designed you can attach a logger to any class in your framework. But for simple projects this is a bit overkill.
1: /// <summary>
2: /// Static class logger using Log4Net
3: /// </summary>
4: public static class ExampleLogger
5: { 6: #region Members
7: 8: /// <summary>
9: /// Create a logger for this static class
10: /// </summary>
11: private static readonly ILog logger = LogManager.GetLogger(typeof(ExampleLogger));
12: 13: #endregion Members
14: 15: #region Constructors
16: 17: /// <summary>
18: /// On construct - build logger object
19: /// </summary>
20: static ExampleLogger()
21: { 22: XmlConfigurator.Configure(); 23: } 24: 25: #endregion Constructors
26: 27: #region Class Methods
28: 29: /// <summary>
30: /// Write information to log
31: /// </summary>
32: /// <param name="logLevel">Level of information to write</param>
33: /// <param name="message">Information to write to log</param>
34: public static void WriteLog(LogLevel logLevel, String message)
35: { 36: if (logLevel.Equals(LogLevel.Debug))
37: { 38: logger.Debug(message); 39: } 40: else if (logLevel.Equals(LogLevel.Error))
41: { 42: logger.Error(message); 43: } 44: else if (logLevel.Equals(LogLevel.Fatal))
45: { 46: logger.Fatal(message); 47: } 48: else if (logLevel.Equals(LogLevel.Info))
49: { 50: logger.Info(message); 51: } 52: else if (logLevel.Equals(LogLevel.Warn))
53: { 54: logger.Warn(message); 55: } 56: } 57: 58: #endregion Class Methods
59: } 60: 61: /// <summary>
62: /// Priority level of the information to be logged
63: /// </summary>
64: public enum LogLevel
65: { 66: Debug = 1, 67: Error, 68: Fatal, 69: Info, 70: Warn 71: }You also need to set where the log information is configured in your web config file at the entry point of your application. I.e. either main or Global.asax etc.
1: //Configuration for Log4Net
2: XmlConfigurator.Configure(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
Finally you need to add the log4net configuration section in the web.config
1: <!-- Log 4 Net logging --> 2: <log4net> 3: 4: <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
5: <file value="C:\Log\ExampleLog" />
6: <appendToFile value="true" />
7: <staticLogFileName value="false" />
8: <rollingStyle value="Composite" />
9: <datePattern value="yyyyMMdd.lo\g" />
10: <maxSizeRollBackups value="10" />
11: <maximumFileSize value="1MB" />
12: <layout type="log4net.Layout.PatternLayout">
13: <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
14: </layout> 15: </appender> 16: 17: <root>18: <level value="DEBUG" />
19: <appender-ref ref="RollingFileAppender" />
20: </root> 21: 22: </log4net>This configuration sections will log information to a file named ExampleLog and a data pattern inserted. Each day a new file will be created. As if the log file exceeds 1MB.
For more examples for configurations for log4Net – see the website:

No comments:
Post a Comment