Skip to content

Instantly share code, notes, and snippets.

@martinabrahams
Created March 28, 2016 01:49
Show Gist options
  • Save martinabrahams/a66906f2fccd3efd4ed3 to your computer and use it in GitHub Desktop.
Save martinabrahams/a66906f2fccd3efd4ed3 to your computer and use it in GitHub Desktop.
Create a custom SQL error log for ELMAH. Append data stored in HttpContext.Items to log entries to provide critical debugging info.
using Elmah;
using System;
using System.Collections;
using System.Text;
namespace MyProject
{
public class ElmahCustomSqlErrorLog : SqlErrorLog
{
public ElmahCustomSqlErrorLog(IDictionary config) : base(config) { }
public ElmahCustomSqlErrorLog(string connectionString) : base(connectionString) { }
public override string Log(Error error)
{
if (error == null)
throw new ArgumentNullException("error");
var webserviceLog = System.Web.HttpContext.Current.Items["CustomItem"] as StringBuilder;
if (webserviceLog != null)
{
error.Detail += "\r\n\r\n-- Custom Entry Log --\r\n\r\n" + webserviceLog.ToString();
}
return base.Log(error);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<elmah>
<errorLog type="ElmahCustomSqlErrorLog, MyProject" connectionStringName="DefaultConnection" applicationName="MyProject" />
</elmah>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment