Skip to content

Commit

Permalink
Add support for configuring Rollbar from the NLog target and using fo…
Browse files Browse the repository at this point in the history
…rmatting
  • Loading branch information
mroach committed May 21, 2013
1 parent 34e5dbc commit 454aeb4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/RollbarSharp.NLog/RollbarSharp.NLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<HintPath>..\packages\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
Expand Down
87 changes: 77 additions & 10 deletions src/RollbarSharp.NLog/RollbarTarget.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,99 @@
using NLog;
using NLog.Common;
using NLog.Layouts;
using NLog.Targets;

namespace RollbarSharp.NLog
{
[Target("Rollbar")]
public class RollbarTarget : TargetWithLayout
{
protected RollbarClient RollbarClient { get; set; }
public string AccessToken { get; set; }

public string Endpoint { get; set; }

public Layout Environment { get; set; }

public Layout Platform { get; set; }

public Layout Language { get; set; }

public Layout Framework { get; set; }


public Layout Title { get; set; }


public RollbarTarget()
{
RollbarClient = new RollbarClient();
RollbarClient.RequestCompleted += RollbarClientRequestCompleted;
Title = "${message}";
}

protected override void Write(LogEventInfo logEvent)
{
var client = CreateClient(logEvent);
var level = ConvertLogLevel(logEvent.Level);
var title = Title.Render(logEvent);

var notice = logEvent.Exception != null
? client.NoticeBuilder.CreateExceptionNotice(logEvent.Exception)
: client.NoticeBuilder.CreateMessageNotice(logEvent.Message);

notice.Level = level;
notice.Title = title;

client.Send(notice);
}

/// <summary>
/// First attemps to create a RollbarClient using config read from
/// appSettings. Uses properties of this class as overrides if specified.
/// </summary>
/// <param name="logEvent"></param>
/// <returns></returns>
private RollbarClient CreateClient(LogEventInfo logEvent)
{
var client = new RollbarClient();
client.RequestStarting += RollbarClientRequestStarting;
client.RequestCompleted += RollbarClientRequestCompleted;

if (logEvent.Exception != null)
{
RollbarClient.SendException(logEvent.Exception, logEvent.FormattedMessage, level);
return;
}

RollbarClient.SendMessage(logEvent.FormattedMessage, level);
if (!string.IsNullOrEmpty(AccessToken))
client.Configuration.AccessToken = AccessToken;

if (!string.IsNullOrEmpty(Endpoint))
client.Configuration.Endpoint = Endpoint;

if (Environment != null)
client.Configuration.Environment = Environment.Render(logEvent);

if (Platform != null)
client.Configuration.Platform = Platform.Render(logEvent);

if (Language != null)
client.Configuration.Language = Language.Render(logEvent);

if (Framework != null)
client.Configuration.Framework = Framework.Render(logEvent);

return client;
}

/// <summary>
/// When posting the request to Rollbar, log it to the internal NLog log
/// </summary>
/// <param name="source"></param>
/// <param name="args"></param>
private static void RollbarClientRequestStarting(object source, RequestStartingEventArgs args)
{
var client = (RollbarClient)source;
InternalLogger.Debug("Sending request to {0}: {1}", client.Configuration.Endpoint, args.Payload);
}

/// <summary>
/// When receiving a response from Rollbar, log it to the internal NLog log
/// </summary>
/// <param name="source"></param>
/// <param name="args"></param>
private static void RollbarClientRequestCompleted(object source, RequestCompletedEventArgs args)
{
if (args.Result.IsSuccess)
Expand Down

0 comments on commit 454aeb4

Please sign in to comment.