Created
June 26, 2014 20:12
-
-
Save pbering/dc35f94360d73252a052 to your computer and use it in GitHub Desktop.
Log4Net Appender that submits errors to NewRelic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using log4net.Appender; | |
using log4net.spi; | |
using Sitecore.Data.Items; | |
namespace SitecoreDiagnostics.NewRelic | |
{ | |
public class Log4NetErrorAppender : AppenderSkeleton | |
{ | |
protected override void Append(LoggingEvent loggingEvent) | |
{ | |
if (!Configuration.NewRelicEnabled) | |
{ | |
return; | |
} | |
if (loggingEvent.Level < Level.ERROR) | |
{ | |
return; | |
} | |
// Skip exceptions | |
var isException = !string.IsNullOrEmpty(loggingEvent.GetExceptionStrRep()); | |
if (isException) | |
{ | |
return; | |
} | |
IDictionary<string, string> parameters = new Dictionary<string, string>(); | |
parameters.Add("Level", loggingEvent.Level.ToString()); | |
parameters.Add("LoggerName", loggingEvent.LoggerName); | |
parameters.Add("ThreadName", loggingEvent.ThreadName); | |
parameters.Add("Machine", Environment.MachineName); | |
parameters.Add("Identity", loggingEvent.Identity); | |
string siteName; | |
if (SitecoreContextHelper.TryGetSiteName(out siteName)) | |
{ | |
parameters.Add("Site", siteName); | |
} | |
string languageName; | |
if (SitecoreContextHelper.TryGetLanguageName(out languageName)) | |
{ | |
parameters.Add("Language", languageName); | |
} | |
string databaseName; | |
if (SitecoreContextHelper.TryGetDatabaseName(out databaseName)) | |
{ | |
parameters.Add("Database", databaseName); | |
} | |
string deviceName; | |
if (SitecoreContextHelper.TryGetDeviceName(out deviceName)) | |
{ | |
parameters.Add("Device", deviceName); | |
} | |
Item item; | |
if (SitecoreContextHelper.TryGetItem(out item)) | |
{ | |
parameters.Add("ItemPath", item.Paths.FullPath); | |
global::NewRelic.Api.Agent.NewRelic.SetTransactionName("Custom", TransactionNaming.GetName(item, deviceName)); | |
} | |
global::NewRelic.Api.Agent.NewRelic.NoticeError(loggingEvent.MessageObject.ToString(), parameters); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment