In this blog I would like to show how I usually log my exceptions using a cool free third party control called log4net. This control is a dll file that can be dropped in the bin folder of the application and with few a configurations in the Web.config file you will be able to write to 3 logging location (system file, event viewer, and in SQL database) and possibly more.
and it is open source.
After download the zip file you will find the API file log4net.dll here: ..\incubating-log4net-1.2.10\log4net-1.2.10\bin\net\2.0\release
Copy the dll to your application bin folder, assuming it is a web application.
You will need to make a reference to the dll from your project. In Visual Studio right click on references, click add reference navigate to the log4net.dll then click add. This will make a reference to the log for net APIs.
Now you need configure you web.config file for API to log the your messages to the proper location.
Setting up web.config for SQL server logging
create a table in your SQL database by running this code.
CREATE TABLE [dbo].[Log] ( [Id] [int] IDENTITY (1, 1) NOT NULL, [Date] [datetime] NOT NULL, [Thread] [varchar] (255) NOT NULL, [Level] [varchar] (50) NOT NULL, [Logger] [varchar] (255) NOT NULL, [Message] [varchar] (4000) NOT NULL, [Exception] [varchar] (2000) NULL )
Now configure you web.config
in the configuration section add
<configSections>
<sectionGroup ...
...
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
little below in the web.config file add the configuration, you can copy and paste and change only the following sections connection string.
<log4net>
<appender name="SQL" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=127.0.0.1;Initial Catalog=MyDBName;Persist Security Info=True;Integrated Security=True;" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<!-- Loggers -->
<root>
<level value="DEBUG"/>
<appender-ref ref="SQL"/>
</root>
</appender>
Above we entered the configuration for SQL only.
You can also add different appenders such as event log and file appenders. as you can see by adding the following
Setting up web.config for Event Viewer logging
event log appenders: the account running the app needs permission to write to the event log
<appender name="EventLog" type="log4net.Appender.EventLogAppender" >
<applicationName value="MyApplication" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
the root node now add event log appender
<root>
<level value="DEBUG"/>
<appender-ref ref="SQL"/>
<appender-ref ref="EventLog"/>
</root>
Setting up web.config for file appenders
file appenders: the account running the app needs persmission to write to the file where the logs will be entered.
<appender name="File" type="log4net.Appender.RollingFileAppender">
<file value="logs/MyLogFile.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="30"/>
<maximumFileSize value="10MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] %logger - %message%newline"/>
</layout>
</appender>
the root node now add file log appender
<root>
<level value="DEBUG"/>
<appender-ref ref="File"/>
<appender-ref ref="SQL"/>
<appender-ref ref="EventLog"/>
</root>
The Code to Log
Now that the configuration is completed the code to log is pretty simple, here an example on how I use it.
in you class you add the name space on top of the class
using log4net;
using log4net.Config;
Create and log to log of type MyClass
private static readonly ILog log = LogManager.GetLogger(typeof(MyClass))
XmlConfigurator.Configure();
now you can log in different levels by calling any of those methods.
log.Debug("debug message");
log.Error("error message");
log.Info("info message");
log.Warn("warning message");
I hopes this can help you start to log errors and other information in app. If you need more information you can check out this site http://logging.apache.org/log4net/release/manual/configuration.html
Good luck
--
Deylo