1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using Exceptionless . Enrichments ;
5
+ using Exceptionless . Models ;
4
6
using NLog ;
7
+ using NLog . Fluent ;
5
8
6
9
namespace Exceptionless . NLog {
7
10
public static class ExceptionlessClientExtensions {
8
11
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
+ }
10
25
builder . Target . Date = ev . TimeStamp ;
11
26
12
27
if ( ! String . IsNullOrWhiteSpace ( ev . FormattedMessage ) )
@@ -15,8 +30,12 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
15
30
if ( ev . Exception != null )
16
31
builder . SetSource ( ev . LoggerName ) ;
17
32
33
+ var tagList = ev . GetTags ( ) ;
34
+ if ( tagList . Count > 0 )
35
+ builder . AddTags ( tagList . ToArray ( ) ) ;
36
+
18
37
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 ) ;
20
39
21
40
return builder ;
22
41
}
@@ -25,10 +44,59 @@ public static void SubmitFromLogEvent(this ExceptionlessClient client, LogEventI
25
44
CreateFromLogEvent ( client , ev ) . Submit ( ) ;
26
45
}
27
46
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
+
28
94
private static readonly List < string > _ignoredEventProperties = new List < string > {
29
95
"CallerFilePath" ,
30
96
"CallerMemberName" ,
31
- "CallerLineNumber"
97
+ "CallerLineNumber" ,
98
+ "Tags" ,
99
+ "ContextData"
32
100
} ;
33
101
}
34
102
}
0 commit comments