Skip to content

Commit bebc2b4

Browse files
committed
moving to use IExternalScopeProvider; checking state and scope for FunctionName
1 parent 4a9aa46 commit bebc2b4

File tree

19 files changed

+220
-269
lines changed

19 files changed

+220
-269
lines changed

src/WebJobs.Script.WebHost/Diagnostics/AzureMonitorDiagnosticLogger.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using Microsoft.Azure.WebJobs.Logging;
78
using Microsoft.Extensions.Logging;
@@ -62,6 +63,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
6263
(string exceptionType, string exceptionMessage, string exceptionDetails) = exception.GetExceptionDetails();
6364

6465
var scopeProps = _scopeProvider.GetScopeDictionary();
66+
var stateProps = state as IEnumerable<KeyValuePair<string, object>> ?? new Dictionary<string, object>();
6567

6668
// Build up a JSON string for the Azure Monitor 'properties' bag
6769
StringWriter sw = new StringWriter();
@@ -72,7 +74,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
7274
WritePropertyIfNotNull(writer, "category", _category);
7375
WritePropertyIfNotNull(writer, "hostVersion", _hostVersion);
7476
WritePropertyIfNotNull(writer, "functionInvocationId", Utility.GetValueFromScope(scopeProps, ScopeKeys.FunctionInvocationId));
75-
WritePropertyIfNotNull(writer, "functionName", Utility.GetValueFromScope(scopeProps, ScopeKeys.FunctionName));
77+
WritePropertyIfNotNull(writer, "functionName", Utility.ResolveFunctionName(stateProps, scopeProps));
7678
WritePropertyIfNotNull(writer, "hostInstanceId", _hostInstanceId);
7779
WritePropertyIfNotNull(writer, "activityId", Utility.GetValueFromScope(scopeProps, ScriptConstants.LogPropertyActivityIdKey));
7880
WritePropertyIfNotNull(writer, "level", logLevel.ToString());

src/WebJobs.Script.WebHost/Diagnostics/DictionaryLoggerScope.cs

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/WebJobs.Script.WebHost/Diagnostics/SystemLogger.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@ public class SystemLogger : ILogger
2222
private readonly LogLevel _logLevel;
2323
private readonly IDebugStateProvider _debugStateProvider;
2424
private readonly IScriptEventManager _eventManager;
25+
private readonly IExternalScopeProvider _scopeProvider;
2526

26-
public SystemLogger(string hostInstanceId, string categoryName, IEventGenerator eventGenerator, IEnvironment environment, IDebugStateProvider debugStateProvider, IScriptEventManager eventManager)
27+
public SystemLogger(string hostInstanceId, string categoryName, IEventGenerator eventGenerator, IEnvironment environment,
28+
IDebugStateProvider debugStateProvider, IScriptEventManager eventManager, IExternalScopeProvider scopeProvider)
2729
{
2830
_environment = environment;
2931
_eventGenerator = eventGenerator;
3032
_categoryName = categoryName ?? string.Empty;
3133
_logLevel = LogLevel.Debug;
32-
_functionName = LogCategories.IsFunctionCategory(_categoryName) ? _categoryName.Split('.')[1] : string.Empty;
34+
_functionName = LogCategories.IsFunctionCategory(_categoryName) ? _categoryName.Split('.')[1] : null;
3335
_isUserFunction = LogCategories.IsFunctionUserCategory(_categoryName);
3436
_hostInstanceId = hostInstanceId;
3537
_debugStateProvider = debugStateProvider;
3638
_eventManager = eventManager;
39+
_scopeProvider = scopeProvider;
3740
}
3841

39-
public IDisposable BeginScope<TState>(TState state) => DictionaryLoggerScope.Push(state);
42+
public IDisposable BeginScope<TState>(TState state) => _scopeProvider.Push(state);
4043

4144
public bool IsEnabled(LogLevel logLevel)
4245
{
@@ -63,7 +66,9 @@ private bool IsUserLog<TState>(TState state)
6366
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
6467
{
6568
// propagate special exceptions through the EventManager
66-
string source = _categoryName ?? Utility.GetValueFromState(state, ScriptConstants.LogPropertySourceKey);
69+
var stateProps = state as IEnumerable<KeyValuePair<string, object>> ?? new Dictionary<string, object>();
70+
71+
string source = _categoryName ?? Utility.GetStateValueOrDefault<string>(stateProps, ScriptConstants.LogPropertySourceKey);
6772
if (exception is FunctionIndexingException && _eventManager != null)
6873
{
6974
_eventManager.Publish(new FunctionIndexingEvent("FunctionIndexingException", source, exception));
@@ -83,7 +88,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
8388
return;
8489
}
8590

86-
IDictionary<string, object> scopeProps = DictionaryLoggerScope.GetMergedStateDictionary() ?? new Dictionary<string, object>();
91+
IDictionary<string, object> scopeProps = _scopeProvider.GetScopeDictionary();
8792

8893
// Apply standard event properties
8994
// Note: we must be sure to default any null values to empty string
@@ -93,11 +98,11 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
9398
string summary = Sanitizer.Sanitize(formattedMessage) ?? string.Empty;
9499
string innerExceptionType = string.Empty;
95100
string innerExceptionMessage = string.Empty;
96-
string functionName = _functionName;
97-
string eventName = !string.IsNullOrEmpty(eventId.Name) ? eventId.Name : Utility.GetValueFromState(state, ScriptConstants.LogPropertyEventNameKey);
101+
string functionName = _functionName ?? Utility.ResolveFunctionName(stateProps, scopeProps) ?? string.Empty;
102+
string eventName = !string.IsNullOrEmpty(eventId.Name) ? eventId.Name : Utility.GetStateValueOrDefault<string>(stateProps, ScriptConstants.LogPropertyEventNameKey) ?? string.Empty;
98103
string functionInvocationId = Utility.GetValueFromScope(scopeProps, ScriptConstants.LogPropertyFunctionInvocationIdKey) ?? string.Empty;
99104
string hostInstanceId = _hostInstanceId;
100-
string activityId = Utility.GetValueFromState(state, ScriptConstants.LogPropertyActivityIdKey);
105+
string activityId = Utility.GetStateValueOrDefault<string>(stateProps, ScriptConstants.LogPropertyActivityIdKey) ?? string.Empty;
101106
string runtimeSiteName = _environment.GetRuntimeSiteName() ?? string.Empty;
102107

103108
// Populate details from the exception.

src/WebJobs.Script.WebHost/Diagnostics/SystemLoggerProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
1313
{
14-
public class SystemLoggerProvider : ILoggerProvider
14+
public class SystemLoggerProvider : ILoggerProvider, ISupportExternalScope
1515
{
1616
private readonly string _hostInstanceId;
1717
private readonly IEventGenerator _eventGenerator;
1818
private readonly IEnvironment _environment;
1919
private readonly IDebugStateProvider _debugStateProvider;
2020
private readonly IScriptEventManager _eventManager;
21+
private IExternalScopeProvider _scopeProvider;
2122

2223
public SystemLoggerProvider(IOptions<ScriptJobHostOptions> scriptOptions, IEventGenerator eventGenerator, IEnvironment environment, IDebugStateProvider debugStateProvider, IScriptEventManager eventManager)
2324
: this(scriptOptions.Value.InstanceId, eventGenerator, environment, debugStateProvider, eventManager)
@@ -40,7 +41,7 @@ public ILogger CreateLogger(string categoryName)
4041
// The SystemLogger is not used for user logs.
4142
return NullLogger.Instance;
4243
}
43-
return new SystemLogger(_hostInstanceId, categoryName, _eventGenerator, _environment, _debugStateProvider, _eventManager);
44+
return new SystemLogger(_hostInstanceId, categoryName, _eventGenerator, _environment, _debugStateProvider, _eventManager, _scopeProvider);
4445
}
4546

4647
private bool IsUserLogCategory(string categoryName)
@@ -51,5 +52,10 @@ private bool IsUserLogCategory(string categoryName)
5152
public void Dispose()
5253
{
5354
}
55+
56+
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
57+
{
58+
_scopeProvider = scopeProvider;
59+
}
5460
}
5561
}

src/WebJobs.Script/Diagnostics/DictionaryLoggerScope.cs

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/WebJobs.Script/Diagnostics/Extensions/ScriptHostLoggerExtension.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics;
65
using Microsoft.Extensions.Logging;
76

87
namespace Microsoft.Azure.WebJobs.Script.Diagnostics.Extensions
@@ -17,17 +16,11 @@ internal static class ScriptHostLoggerExtension
1716
new EventId(400, nameof(HostIdIsSet)),
1817
"Host id explicitly set in configuration. This is not a recommended configuration and may lead to unexpected behavior.");
1918

20-
private static readonly Action<ILogger, string, Exception> _startingHost =
21-
LoggerMessage.Define<string>(
22-
LogLevel.Information,
23-
new EventId(401, nameof(StartingHost)),
24-
"{message}");
25-
26-
private static readonly Action<ILogger, string, Exception> _functionsErrors =
27-
LoggerMessage.Define<string>(
19+
private static readonly Action<ILogger, string, string, Exception> _functionError =
20+
LoggerMessage.Define<string, string>(
2821
LogLevel.Error,
29-
new EventId(402, nameof(FunctionsErrors)),
30-
"{message}");
22+
new EventId(402, nameof(FunctionError)),
23+
"The '{functionName}' function is in error: {errorMessage}");
3124

3225
private static readonly Action<ILogger, Exception> _addingDescriptorProvidersForAllLanguages =
3326
LoggerMessage.Define(
@@ -100,15 +93,17 @@ public static void HostIdIsSet(this ILogger logger)
10093
_hostIdIsSet(logger, null);
10194
}
10295

103-
public static void StartingHost(this ILogger logger, string hostId, string instanceId, string version, bool inDebugMode, bool inDiagnosticMode, string extensionVersion)
96+
public static void StartingHost(this ILogger logger, string hostId, string instanceId, string version, int processId, int appDomainId, bool inDebugMode, bool inDiagnosticMode, string extensionVersion)
10497
{
105-
string message = $"Starting Host (HostId={hostId}, InstanceId={instanceId}, Version={version}, ProcessId={Process.GetCurrentProcess().Id}, AppDomainId={AppDomain.CurrentDomain.Id}, InDebugMode={inDebugMode}, InDiagnosticMode={inDiagnosticMode}, FunctionsExtensionVersion={extensionVersion})";
106-
_startingHost(logger, message, null);
98+
// LoggerMessage.Define can only handle a max of 6 parameters, so log this directly.
99+
logger.LogInformation(new EventId(401, "StartingHost"),
100+
"Starting Host (HostId={hostId}, InstanceId={instanceId}, Version={version}, ProcessId={processId}, AppDomainId={appDomainId}, InDebugMode={inDebugMode}, InDiagnosticMode={inDiagnosticMode}, FunctionsExtensionVersion={extensionVersion})",
101+
hostId, instanceId, version, processId, appDomainId, inDebugMode, inDiagnosticMode, extensionVersion);
107102
}
108103

109-
public static void FunctionsErrors(this ILogger logger, string message)
104+
public static void FunctionError(this ILogger logger, string functionName, string errorMessage)
110105
{
111-
_functionsErrors(logger, message, null);
106+
_functionError(logger, functionName, errorMessage, null);
112107
}
113108

114109
public static void AddingDescriptorProvidersForAllLanguages(this ILogger logger)

src/WebJobs.Script/Diagnostics/FileLogger.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ internal class FileLogger : ILogger
1717
private readonly Func<bool> _isPrimary;
1818
private readonly string _categoryName;
1919
private readonly LogType _logType;
20+
private readonly IExternalScopeProvider _scopeProvider;
2021

21-
public FileLogger(string categoryName, FileWriter fileWriter, Func<bool> isFileLoggingEnabled, Func<bool> isPrimary, LogType logType)
22+
public FileLogger(string categoryName, FileWriter fileWriter, Func<bool> isFileLoggingEnabled, Func<bool> isPrimary, LogType logType, IExternalScopeProvider scopeProvider)
2223
{
2324
_fileWriter = fileWriter;
2425
_isFileLoggingEnabled = isFileLoggingEnabled;
2526
_isPrimary = isPrimary;
2627
_categoryName = categoryName;
2728
_logType = logType;
29+
_scopeProvider = scopeProvider ?? throw new ArgumentNullException(nameof(scopeProvider));
2830
}
2931

30-
public IDisposable BeginScope<TState>(TState state) => DictionaryLoggerScope.Push(state);
32+
public IDisposable BeginScope<TState>(TState state) => _scopeProvider.Push(state);
3133

3234
public bool IsEnabled(LogLevel logLevel)
3335
{

0 commit comments

Comments
 (0)