Skip to content

Commit 4f63506

Browse files
committed
Added ability to set contextdata with the nlog target.
1 parent d3f15bf commit 4f63506

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed
Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Exceptionless.Enrichments;
5+
using Exceptionless.Models;
46
using NLog;
7+
using NLog.Fluent;
58

69
namespace Exceptionless.NLog {
710
public static class ExceptionlessClientExtensions {
811
public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEventInfo ev) {
9-
var builder = ev.Exception != null ? client.CreateException(ev.Exception) : client.CreateLog(ev.LoggerName, ev.FormattedMessage, ev.Level.Name);
12+
var contextData = ev.GetContextData();
13+
14+
if (ev.Exception != null)
15+
contextData.SetException(ev.Exception);
16+
17+
var builder = client.CreateEvent(contextData);
18+
if (ev.Exception == null) {
19+
builder.SetType(Event.KnownTypes.Log);
20+
builder.SetSource(ev.LoggerName);
21+
builder.SetProperty(Event.KnownDataKeys.Level, ev.Level.Name);
22+
} else {
23+
builder.SetType(Event.KnownTypes.Error);
24+
}
1025
builder.Target.Date = ev.TimeStamp;
1126

1227
if (!String.IsNullOrWhiteSpace(ev.FormattedMessage))
@@ -15,8 +30,12 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
1530
if (ev.Exception != null)
1631
builder.SetSource(ev.LoggerName);
1732

33+
var tagList = ev.GetTags();
34+
if (tagList.Count > 0)
35+
builder.AddTags(tagList.ToArray());
36+
1837
foreach (var p in ev.Properties.Where(kvp => !_ignoredEventProperties.Contains(kvp.Key.ToString(), StringComparer.OrdinalIgnoreCase)))
19-
builder.AddObject(p.Value, p.Key.ToString());
38+
builder.SetProperty(p.Key.ToString(), p.Value);
2039

2140
return builder;
2241
}
@@ -25,10 +44,59 @@ public static void SubmitFromLogEvent(this ExceptionlessClient client, LogEventI
2544
CreateFromLogEvent(client, ev).Submit();
2645
}
2746

47+
public static LogBuilder Tag(this LogBuilder builder, params string[] tags) {
48+
var tagList = builder.LogEventInfo.GetTags();
49+
tagList.AddRange(tags);
50+
51+
return builder;
52+
}
53+
54+
public static LogBuilder ContextProperty(this LogBuilder builder, string key, object value) {
55+
var contextData = builder.LogEventInfo.GetContextData();
56+
contextData[key] = value;
57+
58+
return builder;
59+
}
60+
61+
public static LogBuilder MarkUnhandled(this LogBuilder builder, string submissionMethod = null) {
62+
var contextData = builder.LogEventInfo.GetContextData();
63+
contextData.MarkAsUnhandledError();
64+
if (!String.IsNullOrEmpty(submissionMethod))
65+
contextData.SetSubmissionMethod(submissionMethod);
66+
67+
return builder;
68+
}
69+
70+
public static List<string> GetTags(this LogEventInfo ev) {
71+
var tagList = new List<string>();
72+
if (!ev.Properties.ContainsKey("Tags"))
73+
ev.Properties["Tags"] = tagList;
74+
75+
if (ev.Properties.ContainsKey("Tags")
76+
&& ev.Properties["Tags"] is List<string>)
77+
tagList = (List<string>)ev.Properties["Tags"];
78+
79+
return tagList;
80+
}
81+
82+
public static ContextData GetContextData(this LogEventInfo ev) {
83+
var contextData = new ContextData();
84+
if (!ev.Properties.ContainsKey("ContextData"))
85+
ev.Properties["ContextData"] = contextData;
86+
87+
if (ev.Properties.ContainsKey("ContextData")
88+
&& ev.Properties["ContextData"] is ContextData)
89+
contextData = (ContextData)ev.Properties["ContextData"];
90+
91+
return contextData;
92+
}
93+
2894
private static readonly List<string> _ignoredEventProperties = new List<string> {
2995
"CallerFilePath",
3096
"CallerMemberName",
31-
"CallerLineNumber"
97+
"CallerLineNumber",
98+
"Tags",
99+
"ContextData"
32100
};
33101
}
34102
}

Source/Platforms/NLog/ExceptionlessTarget.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Exceptionless.NLog {
88
[Target("Exceptionless")]
99
public class ExceptionlessTarget : TargetWithLayout {
10-
private ExceptionlessClient _client;
10+
private ExceptionlessClient _client = ExceptionlessClient.Default;
1111

1212
public string ApiKey { get; set; }
1313
public string ServerUrl { get; set; }
@@ -24,12 +24,12 @@ protected override void InitializeTarget() {
2424

2525
if (!String.IsNullOrEmpty(ApiKey) || !String.IsNullOrEmpty(ServerUrl))
2626
_client = new ExceptionlessClient(config => {
27-
config.ApiKey = ApiKey;
28-
config.ServerUrl = ServerUrl;
27+
if (!String.IsNullOrEmpty(ApiKey))
28+
config.ApiKey = ApiKey;
29+
if (!String.IsNullOrEmpty(ServerUrl))
30+
config.ServerUrl = ServerUrl;
2931
config.UseInMemoryStorage();
3032
});
31-
else
32-
_client = ExceptionlessClient.Default;
3333
}
3434

3535
protected override void Write(AsyncLogEventInfo info) {

Source/Samples/SampleConsole/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Exceptionless.Log4net;
2323
using Exceptionless.Logging;
2424
using Exceptionless.Models;
25+
using Exceptionless.NLog;
2526
using log4net;
2627
using log4net.Config;
2728
using log4net.Core;
@@ -57,7 +58,7 @@ private static void Main() {
5758

5859
// test NLog
5960
NLog.GlobalDiagnosticsContext.Set("GlobalProp", "GlobalValue");
60-
Log.Info().Message("Hi").Property("LocalProp", "LocalValue").Write();
61+
Log.Info().Message("Hi").Tag("Tag1", "Tag2").Property("LocalProp", "LocalValue").MarkUnhandled("SomeMethod").ContextProperty("Blah", new Event()).Write();
6162

6263
// test log4net
6364
XmlConfigurator.Configure();

0 commit comments

Comments
 (0)