Skip to content

Commit

Permalink
Merge pull request #108 from snakefoot/master
Browse files Browse the repository at this point in the history
NLog AirbrakeTarget with better Layout support and removed BETA
  • Loading branch information
rbalabukh authored May 21, 2020
2 parents e37f029 + f0cb99f commit bc05c28
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Sharpbrake.NLog.Web/Sharpbrake.NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="NLog" Version="4.5.0-rc03" />
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.4'">
<PackageReference Include="NLog" Version="5.0.0-beta11" />
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
Expand Down
51 changes: 36 additions & 15 deletions src/Sharpbrake.NLog/AirbrakeTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NLog;
using System;
using NLog;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
using Sharpbrake.Client;
using Sharpbrake.Client.Model;
Expand Down Expand Up @@ -79,6 +81,11 @@ public class AirbrakeTarget : TargetWithLayout
/// </summary>
public string BlacklistKeys { get; set; }

public AirbrakeTarget()
{
Layout = "${message}";
}

/// <summary>
/// Maps NLog log level onto <see cref="Severity"/> of the error.
/// </summary>
Expand Down Expand Up @@ -115,29 +122,43 @@ protected override void InitializeTarget()

var config = new AirbrakeConfig
{
Environment = Environment,
AppVersion = AppVersion,
ProjectKey = ProjectKey,
ProjectId = ProjectId,
Host = Host,
LogFile = LogFile,
ProxyUri = ProxyUri,
ProxyUsername = ProxyUsername,
ProxyPassword = ProxyPassword,
IgnoreEnvironments = Utils.ParseParameter(IgnoreEnvironments),
WhitelistKeys = Utils.ParseParameter(WhitelistKeys),
BlacklistKeys = Utils.ParseParameter(BlacklistKeys)
Environment = RenderSimpleLayout(Environment, nameof(Environment)),
AppVersion = RenderSimpleLayout(AppVersion, nameof(AppVersion)),
ProjectKey = RenderSimpleLayout(ProjectKey, nameof(ProjectKey)),
ProjectId = RenderSimpleLayout(ProjectId, nameof(ProjectId)),
Host = RenderSimpleLayout(Host, nameof(Host)),
LogFile = RenderSimpleLayout(LogFile, nameof(LogFile)),
ProxyUri = RenderSimpleLayout(ProxyUri, nameof(ProxyUri)),
ProxyUsername = RenderSimpleLayout(ProxyUsername, nameof(ProxyUsername)),
ProxyPassword = RenderSimpleLayout(ProxyPassword, nameof(ProxyPassword)),
IgnoreEnvironments = Utils.ParseParameter(RenderSimpleLayout(IgnoreEnvironments, nameof(IgnoreEnvironments))),
WhitelistKeys = Utils.ParseParameter(RenderSimpleLayout(WhitelistKeys, nameof(WhitelistKeys))),
BlacklistKeys = Utils.ParseParameter(RenderSimpleLayout(BlacklistKeys, nameof(BlacklistKeys))),
};

Notifier = new AirbrakeNotifier(config);
}
}

private string RenderSimpleLayout(string simpleLayout, string propertyName)
{
try
{
return string.IsNullOrEmpty(simpleLayout) ? string.Empty : new SimpleLayout(simpleLayout).Render(LogEventInfo.CreateNullEvent());
}
catch
{
return simpleLayout;
}
}


/// <summary>
/// Notifies Airbrake on the error.
/// </summary>
protected override void Write(LogEventInfo logEvent)
{
var notice = Notifier.BuildNotice(GetErrorSeverity(logEvent.Level), logEvent.Exception, logEvent.FormattedMessage);
var mesage = Layout?.Render(logEvent) ?? logEvent.FormattedMessage;
var notice = Notifier.BuildNotice(GetErrorSeverity(logEvent.Level), logEvent.Exception, mesage);
Notifier.SetHttpContext(notice, GetHttpContext());
Notifier.NotifyAsync(notice);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sharpbrake.NLog/Sharpbrake.NLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="NLog" Version="4.5.0-rc03" />
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.4'">
<PackageReference Include="NLog" Version="5.0.0-beta11" />
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
Expand Down

0 comments on commit bc05c28

Please sign in to comment.