Skip to content

Commit b7cdc10

Browse files
authored
NLog ExceptionlessTarget: ApiKey and ServerUrl and Message and UserIdentity with Layout support (#299)
* ExceptionlessTarget - ApiKey and ServerUrl and Message and and UserIdentity and ReferenceId with Layout support * Resolved review comments from pull-request * Added support for UserIdentityName + UserIdentityEmail * Removed support for UserIdentityEmail
1 parent 7ed9cc3 commit b7cdc10

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

src/Platforms/Exceptionless.NLog/ExceptionlessClientExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
namespace Exceptionless.NLog {
99
public static class ExceptionlessClientExtensions {
1010
public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEventInfo ev) {
11+
return CreateFromLogEvent(client, ev, ev.FormattedMessage);
12+
}
13+
14+
public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEventInfo ev, string formattedMessage) {
1115
if (client == null)
1216
throw new ArgumentNullException(nameof(client));
1317

@@ -20,10 +24,10 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
2024
builder.Target.Date = ev.TimeStamp;
2125
builder.SetSource(ev.LoggerName);
2226

23-
if (ev.Properties.Count > 0) {
27+
if (ev.HasProperties) {
2428
foreach (var property in ev.Properties) {
2529
string propertyKey = property.Key.ToString();
26-
if (_ignoredEventProperties.Contains(propertyKey, StringComparer.OrdinalIgnoreCase))
30+
if (_ignoredEventProperties.Contains(propertyKey))
2731
continue;
2832

2933
if (propertyKey.Equals("@value", StringComparison.OrdinalIgnoreCase)) {
@@ -53,8 +57,8 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
5357
builder.SetType(Event.KnownTypes.Error);
5458
}
5559

56-
if (!String.IsNullOrWhiteSpace(ev.FormattedMessage))
57-
builder.SetMessage(ev.FormattedMessage);
60+
if (!String.IsNullOrWhiteSpace(formattedMessage))
61+
builder.SetMessage(formattedMessage);
5862

5963
var tags = ev.GetTags();
6064
if (tags != null)
@@ -70,10 +74,7 @@ public static void SubmitFromLogEvent(this ExceptionlessClient client, LogEventI
7074
CreateFromLogEvent(client, ev).Submit();
7175
}
7276

73-
private static readonly List<string> _ignoredEventProperties = new List<string> {
74-
"CallerFilePath",
75-
"CallerMemberName",
76-
"CallerLineNumber",
77+
private static readonly HashSet<string> _ignoredEventProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
7778
"Tags",
7879
"ContextData"
7980
};

src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,39 @@
22
using System.Collections.Generic;
33
using NLog;
44
using NLog.Config;
5+
using NLog.Layouts;
56
using NLog.Targets;
67

78
namespace Exceptionless.NLog {
89
[Target("Exceptionless")]
910
public class ExceptionlessTarget : TargetWithLayout {
1011
private ExceptionlessClient _client = ExceptionlessClient.Default;
1112

12-
public string ApiKey { get; set; }
13-
public string ServerUrl { get; set; }
13+
public Layout ApiKey { get; set; }
14+
public Layout ServerUrl { get; set; }
15+
public Layout UserIdentity { get; set; }
16+
public Layout UserIdentityName { get; set; }
1417

1518
[ArrayParameter(typeof(ExceptionlessField), "field")]
1619
public IList<ExceptionlessField> Fields { get; set; }
1720

1821
public ExceptionlessTarget() {
1922
Fields = new List<ExceptionlessField>();
23+
Layout = "${message}";
2024
}
2125

2226
protected override void InitializeTarget() {
2327
base.InitializeTarget();
2428

25-
if (!String.IsNullOrEmpty(ApiKey) || !String.IsNullOrEmpty(ServerUrl))
29+
var apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
30+
var serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent());
31+
32+
if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl))
2633
_client = new ExceptionlessClient(config => {
27-
if (!String.IsNullOrEmpty(ApiKey) && ApiKey != "API_KEY_HERE")
28-
config.ApiKey = ApiKey;
29-
if (!String.IsNullOrEmpty(ServerUrl))
30-
config.ServerUrl = ServerUrl;
34+
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
35+
config.ApiKey = apiKey;
36+
if (!String.IsNullOrEmpty(serverUrl))
37+
config.ServerUrl = serverUrl;
3138
config.UseInMemoryStorage();
3239
});
3340
}
@@ -40,9 +47,15 @@ protected override void Write(LogEventInfo logEvent) {
4047
if (logEvent.Level < minLogLevel)
4148
return;
4249

43-
var builder = _client.CreateFromLogEvent(logEvent);
50+
var formattedMessage = RenderLogEvent(Layout, logEvent);
51+
var builder = _client.CreateFromLogEvent(logEvent, formattedMessage);
52+
53+
var userIdentity = RenderLogEvent(UserIdentity, logEvent);
54+
var userIdentityName = RenderLogEvent(UserIdentityName, logEvent);
55+
builder.Target.SetUserIdentity(userIdentity, userIdentityName);
56+
4457
foreach (var field in Fields) {
45-
var renderedField = field.Layout.Render(logEvent);
58+
var renderedField = RenderLogEvent(field.Layout, logEvent);
4659
if (!String.IsNullOrWhiteSpace(renderedField))
4760
builder.AddObject(renderedField, field.Name);
4861
}

src/Platforms/Exceptionless.NLog/LogEventBuilderExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public static LogEventBuilder MarkUnhandled(this LogEventBuilder builder, string
9090
}
9191

9292
internal static HashSet<string> GetTags(this LogEventInfo ev) {
93-
if (ev.Properties.ContainsKey(Tags) && ev.Properties[Tags] is HashSet<string>)
94-
return (HashSet<string>)ev.Properties[Tags];
93+
if (ev.HasProperties && ev.Properties.TryGetValue(Tags, out var tags))
94+
return tags as HashSet<string>;
9595

9696
return null;
9797
}
@@ -109,8 +109,8 @@ private static void AddTags(this LogEventInfo ev, params string[] tags) {
109109
}
110110

111111
internal static IDictionary<string, object> GetContextData(this LogEventInfo ev) {
112-
if (ev.Properties.ContainsKey(ContextData) && ev.Properties[ContextData] is IDictionary<string, object>)
113-
return (IDictionary<string, object>)ev.Properties[ContextData];
112+
if (ev.HasProperties && ev.Properties.TryGetValue(ContextData, out var contextData))
113+
return contextData as IDictionary<string, object>;
114114

115115
return null;
116116
}

src/Platforms/Exceptionless.NLog/NLogExceptionlessLog.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Exceptionless.Logging;
33
using NLog;
4-
using NLog.Fluent;
54
using LogLevel = Exceptionless.Logging.LogLevel;
65

76
namespace Exceptionless.NLog {
@@ -19,35 +18,35 @@ public void Error(string message, string source = null, Exception exception = nu
1918
if (LogLevel.Error < MinimumLogLevel)
2019
return;
2120

22-
_logger.ForErrorEvent().Message(message).LoggerName(source).Exception(exception).Log();
21+
_logger.ForErrorEvent().Message(message).LoggerName(source).Exception(exception).Log(typeof(NLogExceptionlessLog));
2322
}
2423

2524
public void Info(string message, string source = null) {
2625
if (LogLevel.Info < MinimumLogLevel)
2726
return;
2827

29-
_logger.ForInfoEvent().Message(message).LoggerName(source).Log();
28+
_logger.ForInfoEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog));
3029
}
3130

3231
public void Debug(string message, string source = null) {
3332
if (LogLevel.Debug < MinimumLogLevel)
3433
return;
3534

36-
_logger.ForDebugEvent().Message(message).LoggerName(source).Log();
35+
_logger.ForDebugEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog));
3736
}
3837

3938
public void Warn(string message, string source = null) {
4039
if (LogLevel.Warn < MinimumLogLevel)
4140
return;
4241

43-
_logger.ForWarnEvent().Message(message).LoggerName(source).Log();
42+
_logger.ForWarnEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog));
4443
}
4544

4645
public void Trace(string message, string source = null) {
4746
if (LogLevel.Trace < MinimumLogLevel)
4847
return;
4948

50-
_logger.ForTraceEvent().Message(message).LoggerName(source).Log();
49+
_logger.ForTraceEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog));
5150
}
5251

5352
public void Flush() { }

src/Platforms/Exceptionless.NLog/readme.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ minimum log level that will be used until the client retrieves settings from the
3737
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3838
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3939
  <targets async="true">
40-
    <target xsi:type="Exceptionless, Exceptionless.NLog" name="exceptionless" apiKey="API_KEY_HERE">
40+
    <target type="Exceptionless, Exceptionless.NLog" name="exceptionless" apiKey="API_KEY_HERE">
4141
      <field name="host" layout="${machinename}" />
42-
      <field name="identity" layout="${identity}" />
43-
      <field name="windows-identity" layout="${windows-identity:userName=True:domain=False}" />
4442
      <field name="process" layout="${processname}" />
43+
      <field name="user" layout="${environment-user}" />
4544
    </target>
4645
  </targets>
4746

0 commit comments

Comments
 (0)