1
1
// Copyright 2025 Zethian Inc.
2
- //
2
+ //
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
5
5
// You may obtain a copy of the License at
6
- //
6
+ //
7
7
// http://www.apache.org/licenses/LICENSE-2.0
8
- //
8
+ //
9
9
// Unless required by applicable law or agreed to in writing, software
10
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
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 ;
21
15
using System ;
22
16
using System . Collections . Generic ;
23
17
using System . Dynamic ;
26
20
using System . Net . Http . Headers ;
27
21
using System . Text ;
28
22
using System . Text . Json ;
23
+ using System . Text . Json . Serialization ;
29
24
using System . Threading ;
30
25
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 ;
33
30
using Serilog . Formatting ;
31
+ using Serilog . Sinks . AzureLogAnalytics ;
32
+ using Serilog . Sinks . Batch ;
33
+ using NamingStrategy = Serilog . Sinks . AzureLogAnalytics . NamingStrategy ;
34
34
35
35
namespace Serilog . Sinks
36
36
{
@@ -40,12 +40,12 @@ internal class AzureLogAnalyticsSink : BatchProvider, ILogEventSink
40
40
private DateTimeOffset expire_on = DateTimeOffset . MinValue ;
41
41
private readonly string LoggerUriString ;
42
42
private readonly SemaphoreSlim _semaphore ;
43
- private readonly JsonSerializerOptions _jsonOptions ;
43
+ private readonly LogsJsonSerializerContext _logsJsonSerializerContext ;
44
44
private readonly ConfigurationSettings _configurationSettings ;
45
45
private readonly LoggerCredential _loggerCredential ;
46
46
private static readonly HttpClient httpClient = new HttpClient ( ) ;
47
47
48
- const string scope = "https://monitor.azure.com//.default" ;
48
+ private const string scope = "https://monitor.azure.com//.default" ;
49
49
50
50
internal AzureLogAnalyticsSink ( LoggerCredential loggerCredential , ConfigurationSettings settings , ITextFormatter formatter ) :
51
51
base ( settings . BatchSize , settings . BufferSize )
@@ -56,25 +56,29 @@ internal AzureLogAnalyticsSink(LoggerCredential loggerCredential, ConfigurationS
56
56
57
57
_configurationSettings = settings ;
58
58
59
+ JsonSerializerOptions jsonOptions ;
59
60
switch ( settings . PropertyNamingStrategy )
60
61
{
61
62
case NamingStrategy . Default :
62
- _jsonOptions = new JsonSerializerOptions ( ) ;
63
+ jsonOptions = new JsonSerializerOptions ( ) ;
63
64
64
65
break ;
66
+
65
67
case NamingStrategy . CamelCase :
66
- _jsonOptions = new JsonSerializerOptions ( )
68
+ jsonOptions = new JsonSerializerOptions ( )
67
69
{
68
70
PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
69
71
} ;
70
72
71
73
break ;
74
+
72
75
default : throw new ArgumentOutOfRangeException ( ) ;
73
76
}
74
77
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 ) ;
78
82
if ( _configurationSettings . MaxDepth > 0 )
79
83
{
80
84
_configurationSettings . MaxDepth = _configurationSettings . MaxDepth ;
@@ -90,15 +94,13 @@ public void Emit(LogEvent logEvent)
90
94
PushEvent ( logEvent ) ;
91
95
}
92
96
93
- #endregion
94
-
97
+ #endregion ILogEvent implementation
95
98
96
99
protected override async Task < bool > WriteLogEventAsync ( ICollection < LogEvent > logEventsBatch )
97
100
{
98
101
if ( ( logEventsBatch == null ) || ( logEventsBatch . Count == 0 ) )
99
102
return true ;
100
103
101
-
102
104
var jsonStringCollection = new List < string > ( ) ;
103
105
104
106
var logs = logEventsBatch . Select ( s =>
@@ -112,6 +114,7 @@ protected override async Task<bool> WriteLogEventAsync(ICollection<LogEvent> log
112
114
113
115
return await PostDataAsync ( logs ) ;
114
116
}
117
+
115
118
private async Task < ( string , DateTimeOffset ) > GetAuthToken ( )
116
119
{
117
120
if ( _loggerCredential . TokenCredential != null )
@@ -177,7 +180,7 @@ private async Task<bool> PostDataAsync(IEnumerable<IDictionary<string, object>>
177
180
httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , $ "{ token } ") ;
178
181
}
179
182
180
- var jsonString = JsonSerializer . Serialize ( logs , _jsonOptions ) ;
183
+ var jsonString = JsonSerializer . Serialize ( logs , typeof ( IEnumerable < IDictionary < string , object > > ) , _logsJsonSerializerContext ) ;
181
184
var jsonContent = new StringContent ( jsonString , Encoding . UTF8 , "application/json" ) ;
182
185
183
186
var response = httpClient . PostAsync ( LoggerUriString , jsonContent ) . GetAwaiter ( ) . GetResult ( ) ;
@@ -200,4 +203,4 @@ private async Task<bool> PostDataAsync(IEnumerable<IDictionary<string, object>>
200
203
}
201
204
}
202
205
}
203
- }
206
+ }
0 commit comments