Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.