Skip to content

Commit 1f8e7a6

Browse files
Support AOT
-- set IsAotCompatible= true. It turns on analyzers to check AoT support of code -- use AoT compatible Json serializer
1 parent f61eff2 commit 1f8e7a6

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

src/Serilog.Sinks.AzureLogAnalytics.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
<SignAssembly>True</SignAssembly>
1616
<AssemblyOriginatorKeyFile>Serilog.snk</AssemblyOriginatorKeyFile>
1717
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
18+
<LangVersion>latest</LangVersion>
1819
<RootNamespace>Serilog</RootNamespace>
1920
<PackageIcon>serilog-sink-nuget.png</PackageIcon>
2021
<PackageReadmeFile>README.md</PackageReadmeFile>
2122
</PropertyGroup>
23+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
24+
<IsAotCompatible>true</IsAotCompatible>
25+
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
26+
</PropertyGroup>
2227
<PropertyGroup>
2328
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2429
</PropertyGroup>

src/Sinks/AzureLogAnalytics/AzureLogAnalyticsSink.cs

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
// Copyright 2025 Zethian Inc.
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using Azure.Core;
16-
using Serilog.Core;
17-
using Serilog.Debugging;
18-
using Serilog.Events;
19-
using Serilog.Sinks.AzureLogAnalytics;
20-
using Serilog.Sinks.Batch;
2115
using System;
2216
using System.Collections.Generic;
2317
using System.Dynamic;
@@ -26,11 +20,17 @@
2620
using System.Net.Http.Headers;
2721
using System.Text;
2822
using System.Text.Json;
23+
using System.Text.Json.Serialization;
2924
using System.Threading;
3025
using System.Threading.Tasks;
31-
using NamingStrategy = Serilog.Sinks.AzureLogAnalytics.NamingStrategy;
32-
using System.Text.Json.Serialization;
26+
using Azure.Core;
27+
using Serilog.Core;
28+
using Serilog.Debugging;
29+
using Serilog.Events;
3330
using Serilog.Formatting;
31+
using Serilog.Sinks.AzureLogAnalytics;
32+
using Serilog.Sinks.Batch;
33+
using NamingStrategy = Serilog.Sinks.AzureLogAnalytics.NamingStrategy;
3434

3535
namespace Serilog.Sinks
3636
{
@@ -40,12 +40,12 @@ internal class AzureLogAnalyticsSink : BatchProvider, ILogEventSink
4040
private DateTimeOffset expire_on = DateTimeOffset.MinValue;
4141
private readonly string LoggerUriString;
4242
private readonly SemaphoreSlim _semaphore;
43-
private readonly JsonSerializerOptions _jsonOptions;
43+
private readonly LogsJsonSerializerContext _logsJsonSerializerContext;
4444
private readonly ConfigurationSettings _configurationSettings;
4545
private readonly LoggerCredential _loggerCredential;
4646
private static readonly HttpClient httpClient = new HttpClient();
4747

48-
const string scope = "https://monitor.azure.com//.default";
48+
private const string scope = "https://monitor.azure.com//.default";
4949

5050
internal AzureLogAnalyticsSink(LoggerCredential loggerCredential, ConfigurationSettings settings, ITextFormatter formatter) :
5151
base(settings.BatchSize, settings.BufferSize)
@@ -56,25 +56,29 @@ internal AzureLogAnalyticsSink(LoggerCredential loggerCredential, ConfigurationS
5656

5757
_configurationSettings = settings;
5858

59+
JsonSerializerOptions jsonOptions;
5960
switch (settings.PropertyNamingStrategy)
6061
{
6162
case NamingStrategy.Default:
62-
_jsonOptions = new JsonSerializerOptions();
63+
jsonOptions = new JsonSerializerOptions();
6364

6465
break;
66+
6567
case NamingStrategy.CamelCase:
66-
_jsonOptions = new JsonSerializerOptions()
68+
jsonOptions = new JsonSerializerOptions()
6769
{
6870
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
6971
};
7072

7173
break;
74+
7275
default: throw new ArgumentOutOfRangeException();
7376
}
7477

75-
_jsonOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
76-
_jsonOptions.WriteIndented = false;
77-
_jsonOptions.Converters.Add(new LoggerJsonConverter(formatter));
78+
jsonOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
79+
jsonOptions.WriteIndented = false;
80+
jsonOptions.Converters.Add(new LoggerJsonConverter(formatter));
81+
_logsJsonSerializerContext = new LogsJsonSerializerContext(jsonOptions);
7882
if (_configurationSettings.MaxDepth > 0)
7983
{
8084
_configurationSettings.MaxDepth = _configurationSettings.MaxDepth;
@@ -90,15 +94,13 @@ public void Emit(LogEvent logEvent)
9094
PushEvent(logEvent);
9195
}
9296

93-
#endregion
94-
97+
#endregion ILogEvent implementation
9598

9699
protected override async Task<bool> WriteLogEventAsync(ICollection<LogEvent> logEventsBatch)
97100
{
98101
if ((logEventsBatch == null) || (logEventsBatch.Count == 0))
99102
return true;
100103

101-
102104
var jsonStringCollection = new List<string>();
103105

104106
var logs = logEventsBatch.Select(s =>
@@ -112,6 +114,7 @@ protected override async Task<bool> WriteLogEventAsync(ICollection<LogEvent> log
112114

113115
return await PostDataAsync(logs);
114116
}
117+
115118
private async Task<(string, DateTimeOffset)> GetAuthToken()
116119
{
117120
if (_loggerCredential.TokenCredential != null)
@@ -177,7 +180,7 @@ private async Task<bool> PostDataAsync(IEnumerable<IDictionary<string, object>>
177180
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{token}");
178181
}
179182

180-
var jsonString = JsonSerializer.Serialize(logs, _jsonOptions);
183+
var jsonString = JsonSerializer.Serialize(logs, typeof(IEnumerable<IDictionary<string, object>>), _logsJsonSerializerContext);
181184
var jsonContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
182185

183186
var response = httpClient.PostAsync(LoggerUriString, jsonContent).GetAwaiter().GetResult();
@@ -200,4 +203,4 @@ private async Task<bool> PostDataAsync(IEnumerable<IDictionary<string, object>>
200203
}
201204
}
202205
}
203-
}
206+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Serilog.Sinks.AzureLogAnalytics
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text.Json.Serialization;
6+
using Serilog.Events;
7+
8+
[JsonSerializable(typeof(DateTime))]
9+
[JsonSerializable(typeof(IEnumerable<IDictionary<string, object>>))]
10+
[JsonSerializable(typeof(LogEvent))]
11+
internal partial class LogsJsonSerializerContext : JsonSerializerContext
12+
{
13+
}
14+
}

0 commit comments

Comments
 (0)