@@ -26,7 +26,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget
26
26
/// The messages will be uploaded to the Application Insights cloud service.
27
27
/// </summary>
28
28
[ Target ( "ApplicationInsightsTarget" ) ]
29
- public sealed class ApplicationInsightsTarget : TargetWithLayout
29
+ public sealed class ApplicationInsightsTarget : TargetWithContext
30
30
{
31
31
private TelemetryClient telemetryClient ;
32
32
private DateTime lastLogEventTime ;
@@ -39,6 +39,7 @@ public ApplicationInsightsTarget()
39
39
{
40
40
this . Layout = @"${message}" ;
41
41
this . OptimizeBufferReuse = true ;
42
+ this . IncludeEventProperties = true ;
42
43
}
43
44
44
45
/// <summary>
@@ -50,12 +51,6 @@ public string InstrumentationKey
50
51
set => this . instrumentationKeyLayout = value ?? string . Empty ;
51
52
}
52
53
53
- /// <summary>
54
- /// Gets the array of custom attributes to be passed into the logevent context.
55
- /// </summary>
56
- [ ArrayParameter ( typeof ( TargetPropertyWithContext ) , "contextproperty" ) ]
57
- public IList < TargetPropertyWithContext > ContextProperties { get ; } = new List < TargetPropertyWithContext > ( ) ;
58
-
59
54
/// <summary>
60
55
/// Gets the logging controller we will be using.
61
56
/// </summary>
@@ -90,20 +85,25 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
90
85
propertyBag . Add ( "UserStackFrame" , logEvent . UserStackFrame . ToString ( ) ) ;
91
86
propertyBag . Add ( "UserStackFrameNumber" , logEvent . UserStackFrameNumber . ToString ( CultureInfo . InvariantCulture ) ) ;
92
87
}
93
-
94
- for ( int i = 0 ; i < this . ContextProperties . Count ; ++ i )
88
+ else
95
89
{
96
- var contextProperty = this . ContextProperties [ i ] ;
97
- if ( ! string . IsNullOrEmpty ( contextProperty . Name ) && contextProperty . Layout != null )
98
- {
99
- string propertyValue = this . RenderLogEvent ( contextProperty . Layout , logEvent ) ;
100
- PopulatePropertyBag ( propertyBag , contextProperty . Name , propertyValue ) ;
101
- }
90
+ var callsiteClassName = logEvent . CallerClassName ;
91
+ if ( ! string . IsNullOrEmpty ( callsiteClassName ) )
92
+ propertyBag . Add ( "UserStackClassName" , callsiteClassName ) ;
93
+ var callsiteMemberName = logEvent . CallerMemberName ;
94
+ if ( ! string . IsNullOrEmpty ( callsiteMemberName ) )
95
+ propertyBag . Add ( "UserStackMemberName" , callsiteMemberName ) ;
96
+ var callsiteSourceFilePath = logEvent . CallerFilePath ;
97
+ if ( ! string . IsNullOrEmpty ( callsiteSourceFilePath ) )
98
+ propertyBag . Add ( "UserStackSourceFile" , callsiteSourceFilePath ) ;
99
+ var callsiteSourceLineNumber = logEvent . CallerLineNumber ;
100
+ if ( callsiteSourceLineNumber != 0 )
101
+ propertyBag . Add ( "UserStackSourceLine" , callsiteSourceLineNumber . ToString ( ) ) ;
102
102
}
103
103
104
- if ( logEvent . HasProperties )
104
+ if ( ShouldIncludeProperties ( logEvent ) || ContextProperties . Count > 0 )
105
105
{
106
- LoadLogEventProperties ( logEvent , propertyBag ) ;
106
+ this . GetAllProperties ( logEvent , new StringDictionaryConverter ( propertyBag ) ) ;
107
107
}
108
108
}
109
109
@@ -182,40 +182,6 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
182
182
}
183
183
}
184
184
185
- private static void LoadLogEventProperties ( LogEventInfo logEvent , IDictionary < string , string > propertyBag )
186
- {
187
- if ( logEvent . Properties ? . Count > 0 )
188
- {
189
- foreach ( var keyValuePair in logEvent . Properties )
190
- {
191
- string key = keyValuePair . Key . ToString ( ) ;
192
- object valueObj = keyValuePair . Value ;
193
- PopulatePropertyBag ( propertyBag , key , valueObj ) ;
194
- }
195
- }
196
- }
197
-
198
- private static void PopulatePropertyBag ( IDictionary < string , string > propertyBag , string key , object valueObj )
199
- {
200
- if ( valueObj == null )
201
- {
202
- return ;
203
- }
204
-
205
- string value = Convert . ToString ( valueObj , CultureInfo . InvariantCulture ) ;
206
- if ( propertyBag . ContainsKey ( key ) )
207
- {
208
- if ( string . Equals ( value , propertyBag [ key ] , StringComparison . Ordinal ) )
209
- {
210
- return ;
211
- }
212
-
213
- key += "_1" ;
214
- }
215
-
216
- propertyBag . Add ( key , value ) ;
217
- }
218
-
219
185
private static SeverityLevel ? GetSeverityLevel ( LogLevel logEventLevel )
220
186
{
221
187
if ( logEventLevel == null )
0 commit comments