Spiga

Don't want to GAC log4net.dll

In this example I'll use log4net

I managed to get logging working by dropping the log4net release dll in the _app_bin directory found on my dev system in C:\Inetpub\wwwroot\wss\VirtualDirectories\8081\

The 8081 part is the same as the port I have serving up the sites I am currently playing with.

This is nice to know in a dev environment as you might not want to go to the trouble of GACing (is that a verb?) everything you deliver in you solution.

OK that's fine how do I use it?

My working example extends my previous post on application pages.
Add this code to your web.config (which should be empty at the moment)


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
<root>
<level value="ERROR" />
<!-- priority value can be set to ALL|INFO|WARN|ERROR -->
<priority value="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>

<!-- Define some output appenders -->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=VSMOSSCOMBO;initial catalog=WSS_Log;integrated security=true;" />
<commandText value="INSERT INTO dbo.ExceptionLog ([Date],[Thread],[Level],[Logger],[Message],[Exception],[ApplicationUser]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @user)" />
<parameter>
<parameterName value="@user" />
<dbType value="string" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%X{user}" />
</layout>
</parameter>
<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>
</appender>
</log4net>
</configuration>


This web.config points to a database called WSS_Log with a table called ExceptionLog. Here is the Create Script for the table.


CREATE TABLE [dbo].[ExceptionLog](
[ExceptionLogKey] [int] IDENTITY(1,1) NOT NULL,
[Guid] [uniqueidentifier] NULL,
[ApplicationUser] [nvarchar](255) NULL,
[Date] [datetime] NULL,
[Thread] [nvarchar](255) NULL,
[Level] [nvarchar](50) NULL,
[Logger] [nvarchar](255) NULL,
[Message] [nvarchar](4000) NULL,
[Exception] [nvarchar](2000) NULL,
PRIMARY KEY CLUSTERED
(
[ExceptionLogKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


Add this to your AssemblyInfo.cs


[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

Now you can use the Log but for completeness make your Default.aspx.cs look like this.


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using log4net;
using log4net.Core;

namespace MOSS.Web.Application.Basic
{

[CLSCompliant(false)]
public class Default : LayoutsPageBase
{
private static readonly ILog log = LogManager.GetLogger("Logger");

// add control fields to match controls tags on .aspx page
protected Label lblSiteTitle;
protected Label lblSiteID;

protected override void OnLoad(EventArgs e)
{
log.Debug("OnLoad");

// get current site and web
SPSite siteCollection = this.Site;
SPWeb site = this.Web;

// program against controls on .aspx page
lblSiteTitle.Text = site.Title;
lblSiteID.Text = site.ID.ToString().ToUpper();
}
}
}

0 comments: