Skip to content

Commit

Permalink
adding SystemLogger to WebHost; filtering based on allowed categories
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsam committed Sep 7, 2018
1 parent 64b757f commit 7aa2486
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 169 deletions.
4 changes: 2 additions & 2 deletions sample/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"counterThreshold": 0.80
},
"functionTimeout": "00:05:00",
"logger": {
"fileLoggingMode": "always"
"logging": {
"fileLoggingMode": "always"
},
"swagger": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Logging
{
public static class ILoggingBuilderExtensions
{
public static void AddWebJobsSystem<T>(this ILoggingBuilder builder) where T : SystemLoggerProvider
{
builder.Services.AddSingleton<ILoggerProvider, T>();

// Log all logs to SystemLogger
builder.AddDefaultWebJobsFilters<T>(LogLevel.Trace);
}
}
}
12 changes: 5 additions & 7 deletions src/WebJobs.Script.WebHost/Diagnostics/MetricsEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
Expand All @@ -23,16 +22,16 @@ public class MetricsEventManager : IDisposable
private readonly IEventGenerator _eventGenerator;
private readonly int _functionActivityFlushIntervalSeconds;
private readonly Timer _metricsFlushTimer;
private object _functionActivityTrackerLockObject = new object();
private readonly object _functionActivityTrackerLockObject = new object();
private static string appName;
private static string subscriptionId;
private bool _disposed;

public MetricsEventManager(ScriptSettingsManager settingsManager, IEventGenerator generator, int functionActivityFlushIntervalSeconds, int metricsFlushIntervalMS = DefaultFlushIntervalMS)
public MetricsEventManager(IEnvironment environment, IEventGenerator generator, int functionActivityFlushIntervalSeconds, int metricsFlushIntervalMS = DefaultFlushIntervalMS)
{
// we read these in the ctor (not static ctor) since it can change on the fly
appName = GetNormalizedString(settingsManager.AzureWebsiteUniqueSlotName);
subscriptionId = Utility.GetSubscriptionId(settingsManager) ?? string.Empty;
appName = GetNormalizedString(environment.GetAzureWebsiteUniqueSlotName());
subscriptionId = environment.GetSubscriptionId() ?? string.Empty;

_eventGenerator = generator;
_functionActivityFlushIntervalSeconds = functionActivityFlushIntervalSeconds;
Expand Down Expand Up @@ -497,8 +496,7 @@ private List<FunctionMetrics> GetMetricsQueueSnapshot()

for (int iterator = 0; iterator < currentQueueLength; iterator++)
{
FunctionMetrics queueItem;
if (_functionMetricsQueue.TryDequeue(out queueItem))
if (_functionMetricsQueue.TryDequeue(out FunctionMetrics queueItem))
{
queueSnapshot.Add(queueItem);
}
Expand Down
15 changes: 5 additions & 10 deletions src/WebJobs.Script.WebHost/Diagnostics/SystemLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,22 @@
using System.Collections.Generic;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Logging;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
{
public class SystemLogger : ILogger
{
private readonly IEventGenerator _eventGenerator;
private readonly ScriptSettingsManager _settingsManager;
private readonly string _appName;
private readonly string _subscriptionId;
private readonly string _categoryName;
private readonly string _functionName;
private readonly bool _isUserFunction;
private readonly string _hostInstanceId;
private readonly IEnvironment _environment;

public SystemLogger(string hostInstanceId, string categoryName, IEventGenerator eventGenerator, ScriptSettingsManager settingsManager)
public SystemLogger(string hostInstanceId, string categoryName, IEventGenerator eventGenerator, IEnvironment environment)
{
_settingsManager = settingsManager;
_appName = _settingsManager.AzureWebsiteUniqueSlotName;
_subscriptionId = Utility.GetSubscriptionId(settingsManager);
_environment = environment;
_eventGenerator = eventGenerator;
_categoryName = categoryName ?? string.Empty;
_functionName = LogCategories.IsFunctionCategory(_categoryName) ? _categoryName.Split('.')[1] : string.Empty;
Expand Down Expand Up @@ -74,8 +69,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
// Apply standard event properties
// Note: we must be sure to default any null values to empty string
// otherwise the ETW event will fail to be persisted (silently)
string subscriptionId = _subscriptionId ?? string.Empty;
string appName = _appName ?? string.Empty;
string subscriptionId = _environment.GetSubscriptionId() ?? string.Empty;
string appName = _environment.GetAzureWebsiteUniqueSlotName() ?? string.Empty;
string source = _categoryName ?? Utility.GetValueFromState(state, ScriptConstants.LogPropertySourceKey);
string summary = Sanitizer.Sanitize(formattedMessage) ?? string.Empty;
string innerExceptionType = string.Empty;
Expand Down
18 changes: 11 additions & 7 deletions src/WebJobs.Script.WebHost/Diagnostics/SystemLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using Microsoft.Azure.WebJobs.Logging;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Rpc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -14,14 +13,19 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
public class SystemLoggerProvider : ILoggerProvider
{
private readonly string _hostInstanceId;
private IEventGenerator _eventGenerator;
private ScriptSettingsManager _settingsManager;
private readonly IEventGenerator _eventGenerator;
private readonly IEnvironment _environment;

public SystemLoggerProvider(IOptions<ScriptJobHostOptions> scriptOptions, IEventGenerator eventGenerator, ScriptSettingsManager settingsManager)
public SystemLoggerProvider(IOptions<ScriptJobHostOptions> scriptOptions, IEventGenerator eventGenerator, IEnvironment environment)
: this(scriptOptions.Value.InstanceId, eventGenerator, environment)
{
}

protected SystemLoggerProvider(string hostInstanceId, IEventGenerator eventGenerator, IEnvironment environment)
{
_eventGenerator = eventGenerator;
_settingsManager = settingsManager;
_hostInstanceId = scriptOptions.Value.InstanceId;
_environment = environment;
_hostInstanceId = hostInstanceId;
}

public ILogger CreateLogger(string categoryName)
Expand All @@ -31,7 +35,7 @@ public ILogger CreateLogger(string categoryName)
{
return NullLogger.Instance;
}
return new SystemLogger(_hostInstanceId, categoryName, _eventGenerator, _settingsManager);
return new SystemLogger(_hostInstanceId, categoryName, _eventGenerator, _environment);
}

private bool IsUserLogCategory(string categoryName)
Expand Down

This file was deleted.

16 changes: 5 additions & 11 deletions src/WebJobs.Script.WebHost/Diagnostics/WebHostMetricsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Diagnostics;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
Expand All @@ -12,13 +11,8 @@ public class WebHostMetricsLogger : IMetricsLogger, IDisposable
private readonly MetricsEventManager _metricsEventManager;
private bool disposed = false;

public WebHostMetricsLogger()
: this(ScriptSettingsManager.Instance, new EtwEventGenerator(), 5)
{
}

public WebHostMetricsLogger(IEventGenerator eventGenerator)
: this(ScriptSettingsManager.Instance, eventGenerator, 5)
public WebHostMetricsLogger(IEnvironment environment, IEventGenerator eventGenerator)
: this(environment, eventGenerator, 5)
{
}

Expand All @@ -27,9 +21,9 @@ public WebHostMetricsLogger(MetricsEventManager eventManager)
_metricsEventManager = eventManager;
}

protected WebHostMetricsLogger(ScriptSettingsManager settingsManager, IEventGenerator eventGenerator, int metricEventIntervalInSeconds)
protected WebHostMetricsLogger(IEnvironment environment, IEventGenerator eventGenerator, int metricEventIntervalInSeconds)
{
_metricsEventManager = new MetricsEventManager(settingsManager, eventGenerator, metricEventIntervalInSeconds);
_metricsEventManager = new MetricsEventManager(environment, eventGenerator, metricEventIntervalInSeconds);
}

public object BeginEvent(string eventName, string functionName = null)
Expand Down Expand Up @@ -62,7 +56,7 @@ public void EndEvent(MetricEvent metricEvent)
}
else
{
_metricsEventManager.EndEvent((object)metricEvent);
_metricsEventManager.EndEvent(metricEvent);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
{
/// <summary>
/// A SystemLoggerProvider that can be registered at the WebHost level. It logs with an empty InstanceId
/// to make it clear that it is not a part of a JobHost.
/// </summary>
public class WebHostSystemLoggerProvider : SystemLoggerProvider
{
public WebHostSystemLoggerProvider(IEventGenerator eventGenerator, IEnvironment environment)
: base(string.Empty, eventGenerator, environment)
{
}
}
}
21 changes: 18 additions & 3 deletions src/WebJobs.Script.WebHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -18,8 +19,16 @@ public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args)
.RunAsync()
var host = BuildWebHost(args);

var environment = host.Services.GetService<IEnvironment>();
if (environment.IsLinuxContainerEnvironment())
{
// Linux containers always start out in placeholder mode
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
}

host.RunAsync()
.Wait();
}

Expand Down Expand Up @@ -57,7 +66,13 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args = null)
IsLinuxContainerEnvironment = SystemEnvironment.Instance.IsLinuxContainerEnvironment()
});
})
.ConfigureLogging(b => b.ClearProviders())
.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.ClearProviders();

loggingBuilder.AddDefaultWebJobsFilters();
loggingBuilder.AddWebJobsSystem<WebHostSystemLoggerProvider>();
})
.UseStartup<Startup>();
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/WebJobs.Script.WebHost/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ public void Configure(IApplicationBuilder app, IApplicationLifetime applicationL
{
if (env.IsDevelopment())
{
loggerFactory.AddConsole(LogLevel.Trace, true);
app.UseDeveloperExceptionPage();
}

var environment = app.ApplicationServices.GetService<IEnvironment>();
if (environment.IsLinuxContainerEnvironment())
{
// Linux containers always start out in placeholder mode
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
}

app.UseWebJobsScriptHost(applicationLifetime);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.WebJobs.Script.WebHost
{
Expand Down Expand Up @@ -41,7 +40,8 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();
loggingBuilder.Services.AddSingleton<ILoggerProvider, SystemLoggerProvider>();

loggingBuilder.AddWebJobsSystem<SystemLoggerProvider>();

ConfigureRegisteredBuilders(loggingBuilder, rootServiceProvider);
})
Expand Down
18 changes: 18 additions & 0 deletions src/WebJobs.Script/Environment/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public static string GetAzureWebsiteUniqueSlotName(this IEnvironment environment
return name?.ToLowerInvariant();
}

/// <summary>
/// Gets a the subscription Id of the current site.
/// </summary>
public static string GetSubscriptionId(this IEnvironment environment)
{
string ownerName = environment.GetEnvironmentVariable(AzureWebsiteOwnerName) ?? string.Empty;
if (!string.IsNullOrEmpty(ownerName))
{
int idx = ownerName.IndexOf('+');
if (idx > 0)
{
return ownerName.Substring(0, idx);
}
}

return null;
}

public static bool IsContainerReady(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteContainerReady));
Expand Down
Loading

0 comments on commit 7aa2486

Please sign in to comment.