@@ -29,7 +29,8 @@ public class AzureMonitorDiagnosticLogger : ILogger
29
29
private readonly IEventGenerator _eventGenerator ;
30
30
private readonly IEnvironment _environment ;
31
31
private readonly IExternalScopeProvider _scopeProvider ;
32
- private AppServiceOptions _appServiceOptions ;
32
+ private bool _isAzureMonitorLoggingEnabled ;
33
+ private IOptionsMonitor < AppServiceOptions > _appServiceOptionsMonitor ;
33
34
34
35
public AzureMonitorDiagnosticLogger ( string category , string hostInstanceId , IEventGenerator eventGenerator , IEnvironment environment , IExternalScopeProvider scopeProvider ,
35
36
HostNameProvider hostNameProvider , IOptionsMonitor < AppServiceOptions > appServiceOptionsMonitor )
@@ -40,10 +41,9 @@ public AzureMonitorDiagnosticLogger(string category, string hostInstanceId, IEve
40
41
_environment = environment ?? throw new ArgumentNullException ( nameof ( environment ) ) ;
41
42
_scopeProvider = scopeProvider ?? throw new ArgumentNullException ( nameof ( scopeProvider ) ) ;
42
43
_hostNameProvider = hostNameProvider ?? throw new ArgumentNullException ( nameof ( hostNameProvider ) ) ;
43
- _ = appServiceOptionsMonitor ?? throw new ArgumentNullException ( nameof ( appServiceOptionsMonitor ) ) ;
44
-
45
- appServiceOptionsMonitor . OnChange ( newOptions => _appServiceOptions = newOptions ) ;
46
- _appServiceOptions = appServiceOptionsMonitor . CurrentValue ;
44
+ _appServiceOptionsMonitor = appServiceOptionsMonitor ?? throw new ArgumentNullException ( nameof ( appServiceOptionsMonitor ) ) ;
45
+ UpdateAppServiceOptions ( _appServiceOptionsMonitor . CurrentValue ) ;
46
+ _appServiceOptionsMonitor . OnChange ( UpdateAppServiceOptions ) ;
47
47
48
48
_roleInstance = _environment . GetInstanceId ( ) ;
49
49
@@ -55,7 +55,7 @@ public AzureMonitorDiagnosticLogger(string category, string hostInstanceId, IEve
55
55
public bool IsEnabled ( LogLevel logLevel )
56
56
{
57
57
// We want to instantiate this Logger in placeholder mode to warm it up, but do not want to log anything.
58
- return ! string . IsNullOrEmpty ( _hostNameProvider . Value ) && ! _environment . IsPlaceholderModeEnabled ( ) ;
58
+ return _isAzureMonitorLoggingEnabled && ! string . IsNullOrEmpty ( _hostNameProvider . Value ) && ! _environment . IsPlaceholderModeEnabled ( ) ;
59
59
}
60
60
61
61
public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception exception , Func < TState , Exception , string > formatter )
@@ -107,7 +107,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
107
107
using ( JsonTextWriter writer = new JsonTextWriter ( sw ) { Formatting = Formatting . None } )
108
108
{
109
109
writer . WriteStartObject ( ) ;
110
- WritePropertyIfNotNull ( writer , "appName" , _appServiceOptions . AppName ) ;
110
+ WritePropertyIfNotNull ( writer , "appName" , _appServiceOptionsMonitor . CurrentValue . AppName ) ;
111
111
WritePropertyIfNotNull ( writer , "roleInstance" , _roleInstance ) ;
112
112
WritePropertyIfNotNull ( writer , "message" , formattedMessage ) ;
113
113
WritePropertyIfNotNull ( writer , "category" , _category ) ;
@@ -136,6 +136,11 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
136
136
_eventGenerator . LogAzureMonitorDiagnosticLogEvent ( logLevel , _hostNameProvider . Value , AzureMonitorOperationName , AzureMonitorCategoryName , _regionName , sw . ToString ( ) ) ;
137
137
}
138
138
139
+ private void UpdateAppServiceOptions ( AppServiceOptions appServiceOptions )
140
+ {
141
+ _isAzureMonitorLoggingEnabled = ! ( _environment . IsConsumptionOnLegion ( ) && ! _appServiceOptionsMonitor . CurrentValue . IsAzureMonitorLoggingEnabled ) ;
142
+ }
143
+
139
144
private static void WritePropertyIfNotNull < T > ( JsonTextWriter writer , string propertyName , T propertyValue )
140
145
{
141
146
if ( propertyValue != null )
0 commit comments