Skip to content

Commit dbc79af

Browse files
authored
NLog v6 RTM (#141)
1 parent 00b4d74 commit dbc79af

9 files changed

Lines changed: 61 additions & 51 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ NLog DiagnosticListenerTarget for [Microsoft DiagnosticSource](https://github.co
1313
`Install-Package NLog.DiagnosticSource` or in your csproj:
1414

1515
```xml
16-
<PackageReference Include="NLog.DiagnosticSource" Version="5.*" />
16+
<PackageReference Include="NLog.DiagnosticSource" Version="6.*" />
1717
```
1818

1919
2) Add to your nlog.config:

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 5.0.0.{build}
1+
version: 6.0.0.{build}
22
image: Visual Studio 2022
33
configuration: Release
44
platform: Any CPU

src/NLog.DiagnosticSource/LayoutRenderers/ActivityExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ internal static class ActivityExtensions
1010
private static readonly string EmptySpanIdToHexString = default(System.Diagnostics.ActivitySpanId).ToHexString();
1111
private static readonly string EmptyTraceIdToHexString = default(System.Diagnostics.ActivityTraceId).ToHexString();
1212

13-
public static string GetSpanId(this Activity activity)
13+
public static string? GetSpanId(this Activity activity)
1414
{
1515
return activity.IdFormat == ActivityIdFormat.W3C ?
1616
SpanIdToHexString(activity.SpanId) :
1717
activity.Id;
1818
}
1919

20-
public static string GetTraceId(this Activity activity)
20+
public static string? GetTraceId(this Activity activity)
2121
{
2222
return activity.IdFormat == ActivityIdFormat.W3C ?
2323
TraceIdToHexString(activity.TraceId) :
2424
activity.RootId;
2525
}
2626

27-
public static string GetParentId(this Activity activity)
27+
public static string? GetParentId(this Activity activity)
2828
{
2929
return activity.IdFormat == ActivityIdFormat.W3C ?
3030
SpanIdToHexString(activity.ParentSpanId) :

src/NLog.DiagnosticSource/LayoutRenderers/ActivityTraceLayoutRenderer.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace NLog.LayoutRenderers
1111
[LayoutRenderer("activity")]
1212
public sealed class ActivityTraceLayoutRenderer : LayoutRenderer
1313
{
14-
private static string[] DurationMsFormat = null;
14+
private static string[]? DurationMsFormat = null;
1515

1616
/// <summary>
1717
/// Gets or sets the property to retrieve.
@@ -22,12 +22,12 @@ public sealed class ActivityTraceLayoutRenderer : LayoutRenderer
2222
/// <summary>
2323
/// Single item to extract from <see cref="System.Diagnostics.Activity.Baggage"/> or <see cref="System.Diagnostics.Activity.Tags"/> or with <see cref="System.Diagnostics.Activity.GetCustomProperty(string)"/>
2424
/// </summary>
25-
public string Item { get; set; }
25+
public string? Item { get; set; }
2626

2727
/// <summary>
2828
/// Control output formating of selected property (if supported)
2929
/// </summary>
30-
public string Format { get; set; }
30+
public string? Format { get; set; }
3131

3232
/// <summary>
3333
/// Gets or sets the culture used for rendering.
@@ -167,7 +167,7 @@ private void RenderDurationMs(StringBuilder builder, System.Diagnostics.Activity
167167
}
168168
}
169169

170-
private string GetValue(System.Diagnostics.Activity activity)
170+
private string? GetValue(System.Diagnostics.Activity activity)
171171
{
172172
switch (Property)
173173
{
@@ -183,7 +183,7 @@ private string GetValue(System.Diagnostics.Activity activity)
183183
case ActivityTraceProperty.TraceFlags: return ConvertToString(activity.ActivityTraceFlags, Format);
184184
case ActivityTraceProperty.Baggage: return GetCollectionItem(Item, activity.Baggage);
185185
case ActivityTraceProperty.Tags: return GetCollectionItem(Item, activity.TagObjects);
186-
case ActivityTraceProperty.CustomProperty: return string.IsNullOrEmpty(Item) ? string.Empty : (activity.GetCustomProperty(Item)?.ToString() ?? activity.Parent?.GetCustomProperty(Item)?.ToString() ?? string.Empty);
186+
case ActivityTraceProperty.CustomProperty: return GetCustomProperty(Item, activity);
187187
case ActivityTraceProperty.SourceName: return activity.Source?.Name;
188188
case ActivityTraceProperty.SourceVersion: return activity.Source?.Version;
189189
case ActivityTraceProperty.ActivityKind: return ConvertToString(activity.Kind, Format);
@@ -195,6 +195,14 @@ private string GetValue(System.Diagnostics.Activity activity)
195195
}
196196
}
197197

198+
private static string GetCustomProperty(string? item, System.Diagnostics.Activity activity)
199+
{
200+
if (item is null || string.IsNullOrEmpty(item))
201+
return string.Empty;
202+
203+
return activity.GetCustomProperty(item)?.ToString() ?? activity.Parent?.GetCustomProperty(item)?.ToString() ?? string.Empty;
204+
}
205+
198206
private string GetDuration(System.Diagnostics.Activity activity)
199207
{
200208
var duration = GetDurationTimeSpan(activity);
@@ -234,7 +242,7 @@ private string GetDuration(System.Diagnostics.Activity activity)
234242
return default(TimeSpan?);
235243
}
236244

237-
private static string GetCollectionItem<T>(string item, IEnumerable<KeyValuePair<string, T>> collection) where T : class
245+
private static string GetCollectionItem<T>(string? item, IEnumerable<KeyValuePair<string, T?>> collection) where T : class
238246
{
239247
if (collection is ICollection<KeyValuePair<string, T>> emptyCollection && emptyCollection.Count == 0)
240248
return string.Empty; // Skip allocation of enumerator
@@ -243,14 +251,14 @@ private static string GetCollectionItem<T>(string item, IEnumerable<KeyValuePair
243251
{
244252
if (string.CompareOrdinal(keyValue.Key, item)==0)
245253
{
246-
return ConvertToString(keyValue.Value);
254+
return ConvertToString(keyValue.Value) ?? string.Empty;
247255
}
248256
}
249257

250258
return string.Empty; // Not found
251259
}
252260

253-
private static void RenderStringDictionaryFlat<T>(IEnumerable<KeyValuePair<string, T>> collection, StringBuilder builder) where T : class
261+
private static void RenderStringDictionaryFlat<T>(IEnumerable<KeyValuePair<string, T?>> collection, StringBuilder builder) where T : class
254262
{
255263
if (collection is ICollection<KeyValuePair<string, T>> emptyCollection && emptyCollection.Count == 0)
256264
return; // Skip allocation of enumerator
@@ -263,16 +271,16 @@ private static void RenderStringDictionaryFlat<T>(IEnumerable<KeyValuePair<strin
263271
firstItem = false;
264272
builder.Append(keyValue.Key);
265273

266-
string stringValue = ConvertToString(keyValue.Value);
267-
if (stringValue != null)
268-
{
269-
builder.Append('=');
270-
builder.Append(stringValue);
271-
}
274+
var stringValue = ConvertToString(keyValue.Value);
275+
if (stringValue is null)
276+
continue;
277+
278+
builder.Append('=');
279+
builder.Append(stringValue);
272280
}
273281
}
274282

275-
private static void RenderStringDictionaryJson<T>(IEnumerable<KeyValuePair<string, T>> collection, StringBuilder builder, string dictionaryPrefix = "{ ") where T : class
283+
private static void RenderStringDictionaryJson<T>(IEnumerable<KeyValuePair<string, T?>> collection, StringBuilder builder, string dictionaryPrefix = "{ ") where T : class
276284
{
277285
if (collection is ICollection<KeyValuePair<string, T>> emptyCollection && emptyCollection.Count == 0)
278286
return; // Skip allocation of enumerator
@@ -291,8 +299,8 @@ private static void RenderStringDictionaryJson<T>(IEnumerable<KeyValuePair<strin
291299
builder.Append('"');
292300
builder.Append(EscapeStringQuotes(keyValue.Key));
293301

294-
string stringValue = ConvertToString(keyValue.Value);
295-
if (stringValue == null)
302+
var stringValue = ConvertToString(keyValue.Value);
303+
if (stringValue is null)
296304
builder.Append("\": null");
297305
else
298306
builder.Append("\": \"").Append(EscapeStringQuotes(stringValue)).Append('"');
@@ -346,7 +354,7 @@ private static void RenderActivityEventsJson(StringBuilder builder, IEnumerable<
346354
builder.Append(" ]");
347355
}
348356

349-
private string ConvertToString(System.Diagnostics.ActivityTraceFlags traceFlags, string format)
357+
private string ConvertToString(System.Diagnostics.ActivityTraceFlags traceFlags, string? format)
350358
{
351359
if (string.IsNullOrEmpty(format))
352360
{
@@ -369,7 +377,7 @@ private string ConvertToString(System.Diagnostics.ActivityTraceFlags traceFlags,
369377
return traceFlags.ToString(format);
370378
}
371379

372-
private static string ConvertToString(System.Diagnostics.ActivityKind activityKind, string format)
380+
private static string ConvertToString(System.Diagnostics.ActivityKind activityKind, string? format)
373381
{
374382
if (string.IsNullOrEmpty(format))
375383
{
@@ -398,7 +406,7 @@ private static string ConvertToString(System.Diagnostics.ActivityKind activityKi
398406
return activityKind.ToString(format);
399407
}
400408

401-
private string ConvertToString(System.Diagnostics.ActivityStatusCode statusCode, string format)
409+
private string ConvertToString(System.Diagnostics.ActivityStatusCode statusCode, string? format)
402410
{
403411
if (string.IsNullOrEmpty(format))
404412
{
@@ -423,11 +431,11 @@ private string ConvertToString(System.Diagnostics.ActivityStatusCode statusCode,
423431
return statusCode.ToString(format);
424432
}
425433

426-
private static string ConvertToString(object objectValue)
434+
private static string? ConvertToString(object? objectValue)
427435
{
428436
try
429437
{
430-
if (objectValue == null)
438+
if (objectValue is null)
431439
return null;
432440

433441
return Convert.ToString(objectValue, System.Globalization.CultureInfo.InvariantCulture);
@@ -438,7 +446,7 @@ private static string ConvertToString(object objectValue)
438446
}
439447
}
440448

441-
private static bool FormatEnumAsInteger(string format)
449+
private static bool FormatEnumAsInteger(string? format)
442450
{
443451
return format?.Length == 1 && (format[0] == 'd' || format[0] == 'D');
444452
}

src/NLog.DiagnosticSource/NLog.DiagnosticSource.csproj

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0;net6.0;net462;netstandard2.0</TargetFrameworks>
55
<RootNamespace>NLog</RootNamespace>
6-
<VersionPrefix>5.2.3</VersionPrefix>
6+
<VersionPrefix>6.0.0</VersionPrefix>
77
<VersionSuffix></VersionSuffix>
88

9-
<AssemblyVersion>5.0.0.0</AssemblyVersion>
10-
<!-- AssemblyVersion must be fixed on 5.0.0.0 -->
9+
<AssemblyVersion>6.0.0.0</AssemblyVersion>
10+
<!-- AssemblyVersion must be fixed on 6.0.0.0 -->
1111
<AppVeyorBuildNumber>$(APPVEYOR_BUILD_NUMBER)</AppVeyorBuildNumber>
1212
<AppVeyorBuildNumber Condition="'$(AppVeyorBuildNumber)' == ''">0</AppVeyorBuildNumber>
1313
<FileVersion>$(VersionPrefix).$(AppVeyorBuildNumber)</FileVersion>
@@ -25,29 +25,32 @@
2525
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
2626
<PackageReadmeFile>README.md</PackageReadmeFile>
2727
<PackageReleaseNotes>
28-
- Added support for property IsAllDataRequested (@snakefoot)
28+
- NLog v6 with AOT-build support.
2929

3030
See https://github.com/NLog/NLog.DiagnosticSource for documentation of ${activity} and DiagnosticListenerTarget
3131
</PackageReleaseNotes>
3232

3333
<IsPackable>true</IsPackable>
3434
<GenerateDocumentationFile>true</GenerateDocumentationFile>
35+
<Nullable>enable</Nullable>
3536
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3637
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
37-
<IsTrimmable>true</IsTrimmable>
38+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
39+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
40+
<LangVersion Condition="'$(TargetFramework)'=='net462' Or '$(TargetFramework)'=='netstandard2.0'">9</LangVersion>
3841

3942
<SignAssembly>true</SignAssembly>
4043
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
4144
<DelaySign>false</DelaySign>
4245
</PropertyGroup>
4346

4447
<ItemGroup>
45-
<PackageReference Include="NLog" Version="5.2.2" />
48+
<PackageReference Include="NLog" Version="6.0.0" />
4649
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
4750
</ItemGroup>
4851

4952
<ItemGroup Condition="'$(TargetFramework)'=='net462' Or '$(TargetFramework)'=='netstandard2.0'">
50-
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" />
53+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />
5154
</ItemGroup>
5255

5356
<ItemGroup>

src/NLog.DiagnosticSource/Targets/DiagnosticListenerTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class DiagnosticListenerTarget : TargetWithContext
1717
/// <summary>
1818
/// Source Name for <see cref="DiagnosticSource"/>
1919
/// </summary>
20-
public Layout SourceName { get; set; }
20+
public Layout SourceName { get; set; } = Layout.Empty;
2121

2222
/// <summary>
2323
/// Event Name for <see cref="DiagnosticSource.Write(string, object)"/>
@@ -58,7 +58,7 @@ protected override void Write(LogEventInfo logEvent)
5858
private void WriteLogEventData(DiagnosticSource diagnosticSource, LogEventInfo logEvent, string eventName)
5959
{
6060
var message = RenderLogEvent(Layout, logEvent);
61-
IDictionary<string, object> properties = null;
61+
IDictionary<string, object?>? properties = null;
6262
if (ShouldIncludeProperties(logEvent) || ContextProperties?.Count > 0)
6363
{
6464
properties = GetAllProperties(logEvent);

src/NLog.DiagnosticSource/UnconditionalSuppressMessageAttribute.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ internal sealed class UnconditionalSuppressMessageAttribute : Attribute
134134
/// </summary>
135135
/// <param name="category">The category for the attribute.</param>
136136
/// <param name="checkId">The identifier of the analysis rule the attribute applies to.</param>
137-
public UnconditionalSuppressMessageAttribute(string category, string checkId)
137+
public UnconditionalSuppressMessageAttribute(string? category, string? checkId)
138138
{
139139
Category = category;
140140
CheckId = checkId;
@@ -147,7 +147,7 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
147147
/// The <see cref="Category"/> property describes the tool or tool analysis category
148148
/// for which a message suppression attribute applies.
149149
/// </remarks>
150-
public string Category { get; }
150+
public string? Category { get; }
151151

152152
/// <summary>
153153
/// Gets the identifier of the analysis tool rule to be suppressed.
@@ -156,7 +156,7 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
156156
/// Concatenated together, the <see cref="Category"/> and <see cref="CheckId"/>
157157
/// properties form a unique check identifier.
158158
/// </remarks>
159-
public string CheckId { get; }
159+
public string? CheckId { get; }
160160

161161
/// <summary>
162162
/// Gets or sets the scope of the code that is relevant for the attribute.
@@ -165,7 +165,7 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
165165
/// The Scope property is an optional argument that specifies the metadata scope for which
166166
/// the attribute is relevant.
167167
/// </remarks>
168-
public string Scope { get; set; }
168+
public string? Scope { get; set; }
169169

170170
/// <summary>
171171
/// Gets or sets a fully qualified path that represents the target of the attribute.
@@ -176,7 +176,7 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
176176
/// Because it is fully qualified, it can be long, particularly for targets such as parameters.
177177
/// The analysis tool user interface should be capable of automatically formatting the parameter.
178178
/// </remarks>
179-
public string Target { get; set; }
179+
public string? Target { get; set; }
180180

181181
/// <summary>
182182
/// Gets or sets an optional argument expanding on exclusion criteria.
@@ -188,12 +188,12 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
188188
/// and it may be desirable to suppress a violation against a statement in the method that will
189189
/// give a rule violation, but not against all statements in the method.
190190
/// </remarks>
191-
public string MessageId { get; set; }
191+
public string? MessageId { get; set; }
192192

193193
/// <summary>
194194
/// Gets or sets the justification for suppressing the code analysis message.
195195
/// </summary>
196-
public string Justification { get; set; }
196+
public string? Justification { get; set; }
197197
}
198198
}
199199

test/NLog.DiagnosticSource.Tests/ActivityTraceLayoutRendererTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,14 @@ public void TestAllPropertiesWhenActivityRunning(ActivityTraceProperty property,
143143
public void TestDurationSingleItem(string inputLayout, string expectedResult)
144144
{
145145
var logFactory = new LogFactory();
146-
var xmlStream = new System.IO.StringReader($@"<nlog throwConfigExceptions='true'>
146+
var xmlConfig = $@"<nlog throwConfigExceptions='true'>
147147
<targets>
148148
<target name='Memory' type='Memory' layout='{inputLayout}' />
149149
</targets>
150150
<rules>
151151
<logger name='*' writeTo='Memory' />
152-
</rules></nlog>");
153-
var xmlReader = System.Xml.XmlReader.Create(xmlStream);
154-
var logConfig = new NLog.Config.XmlLoggingConfiguration(xmlReader, null);
152+
</rules></nlog>";
153+
var logConfig = NLog.Config.XmlLoggingConfiguration.CreateFromXmlString(xmlConfig);
155154
logFactory.Configuration = logConfig;
156155
var memTarget = logFactory.Configuration.FindTargetByName<NLog.Targets.MemoryTarget>("Memory");
157156

test/NLog.DiagnosticSource.Tests/NLog.DiagnosticSource.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
1010
<PackageReference Include="xunit" Version="2.9.3" />
1111
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
1212
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)