Skip to content

Instantly share code, notes, and snippets.

@cfurrow
Created March 9, 2012 02:41
Show Gist options
  • Save cfurrow/2004701 to your computer and use it in GitHub Desktop.
Save cfurrow/2004701 to your computer and use it in GitHub Desktop.
public class ApplicationController : Controller
{
//...
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
log4net.GlobalContext.Properties["requestid"] = Guid.NewGuid();
log4net.GlobalContext.Properties["email"] = CurrentUser.Email;
log4net.GlobalContext.Properties["customername"] = CurrentCustomer.Name;
log4net.GlobalContext.Properties["url"] = requestContext.HttpContext.Request.Url.ToString();
}
//...
}
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=somedatabaseserver;Initial Catalog=ZPE;Persist Security Info=True;User ID=sauceybastard;Password=jesusjuiceX12345;timeout=120" />
<!-- THIS IS ALSO IMPORTANT!!! --->
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[RequestId],[Email],[CustomerName],[Url]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @requestid, @email, @customername, @url)" />
<!-- CARRY ON! -->
<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>
<!-- BELOW IS THE INTERESTING STUFF! -->
<parameter>
<parameterName value="@requestid" />
<dbType value="Guid" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="requestid" />
<conversionPattern value="%property{requestid}" />
</layout>
</parameter>
<parameter>
<parameterName value="@email" />
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{email}" />
</layout>
</parameter>
<parameter>
<parameterName value="@customername" />
<dbType value="String"/>
<size value="500"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{customername}" />
</layout>
</parameter>
<parameter>
<parameterName value="@url" />
<dbType value="String"/>
<size value="2048"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{url}" />
</layout>
</parameter>
</appender>
<root>
<level value="all" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
// Copy and paste is easy!
var message = String.Format("/User/Profile/{0} edited by user {1} for customer {2}",userid,email,customername);
Logger.Info(message);
// I'm so clever!
var message = String.Format("/User/Edit/{0} edited by user {1} for customer {2}",userid,email,customername);
Logger.Info(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment