Skip to content

Instantly share code, notes, and snippets.

@j5alive
Last active June 7, 2017 07:38
Show Gist options
  • Save j5alive/87ed7d0b88ce81d65475e512606aacb2 to your computer and use it in GitHub Desktop.
Save j5alive/87ed7d0b88ce81d65475e512606aacb2 to your computer and use it in GitHub Desktop.
Raygun4Net automatic Breadcrumb logging with log4net
<configuration>
<configSections>
<section name="RaygunSettings" type="Mindscape.Raygun4Net.RaygunSettings, Mindscape.Raygun4Net" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<RaygunSettings apikey="[YOUR-API-KEY-HERE]" breadcrumbsLevel="Debug" />
<log4net>
<appender name="breadcrumbAppender" type="Raygun.Examples.RaygunBreadcrumbAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</appender>
<root name="RaygunApp">
<level value="DEBUG" />
<appender-ref ref="breadcrumbAppender" />
</root>
</log4net>
</configuration>
using log4net.Appender;
using log4net.Core;
using Mindscape.Raygun4Net;
using Mindscape.Raygun4Net.Breadcrumbs;
namespace Raygun.Examples
{
public class RaygunBreadcrumbAppender : AppenderSkeleton
{
protected override void Append(LoggingEvent loggingEvent)
{
var crumb = new RaygunBreadcrumb();
int lineNumber;
if (int.TryParse(loggingEvent.LocationInformation.LineNumber, out lineNumber))
{
crumb.LineNumber = lineNumber;
}
crumb.Message = RenderLoggingEvent(loggingEvent);
crumb.ClassName = loggingEvent.LocationInformation.ClassName;
crumb.MethodName = loggingEvent.LocationInformation.MethodName;
crumb.CustomData.Add("Thread", loggingEvent.ThreadName);
if (loggingEvent.Properties != null)
{
foreach (var propertyKey in loggingEvent.Properties.GetKeys())
{
crumb.CustomData.Add(propertyKey, loggingEvent.Properties[propertyKey]);
}
}
switch (loggingEvent.Level.Name)
{
case "ALL":
case "DEBUG":
crumb.Level = RaygunBreadcrumbLevel.Debug;
break;
case "INFO":
crumb.Level = RaygunBreadcrumbLevel.Info;
break;
case "WARN":
crumb.Level = RaygunBreadcrumbLevel.Warning;
break;
case "FATAL":
case "ERROR":
crumb.Level = RaygunBreadcrumbLevel.Error;
break;
}
RaygunClient.RecordBreadcrumb(crumb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment