Permalink
Browse files

Always add event source logger (#287)

  • Loading branch information...
pakrym committed Oct 8, 2018
1 parent ca6ab92 commit 4347a45f292fe340a5e78c9afddde7e6d2f3dd29
View
@@ -25,6 +25,7 @@
<MicrosoftExtensionsLoggingConfigurationPackageVersion>2.2.0-preview3-35425</MicrosoftExtensionsLoggingConfigurationPackageVersion>
<MicrosoftExtensionsLoggingConsolePackageVersion>2.2.0-preview3-35425</MicrosoftExtensionsLoggingConsolePackageVersion>
<MicrosoftExtensionsLoggingDebugPackageVersion>2.2.0-preview3-35425</MicrosoftExtensionsLoggingDebugPackageVersion>
<MicrosoftExtensionsLoggingEventSourcePackageVersion>2.2.0-preview3-35425</MicrosoftExtensionsLoggingEventSourcePackageVersion>
<MicrosoftExtensionsLoggingPackageVersion>2.2.0-preview3-35425</MicrosoftExtensionsLoggingPackageVersion>
<MicrosoftNETCoreApp20PackageVersion>2.0.9</MicrosoftNETCoreApp20PackageVersion>
<MicrosoftNETCoreApp21PackageVersion>2.1.3</MicrosoftNETCoreApp21PackageVersion>
@@ -26,6 +26,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="$(MicrosoftExtensionsLoggingConfigurationPackageVersion)" PrivateAssets="None" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" PrivateAssets="None" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(MicrosoftExtensionsLoggingDebugPackageVersion)" PrivateAssets="None" />
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="$(MicrosoftExtensionsLoggingEventSourcePackageVersion)" PrivateAssets="None" />
</ItemGroup>
</Project>
@@ -190,6 +190,7 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.ConfigureServices((hostingContext, services) =>
{
@@ -2,11 +2,16 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.HostFiltering;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xunit;
@@ -49,6 +54,54 @@ public void WebHostConfiguration_HostFilterOptionsAreReloadable()
Assert.Contains("NewHost", options.AllowedHosts);
}
[Fact]
public void CreateDefaultBuilder_RegistersEventSourceLogger()
{
var listener = new TestEventListener();
var host = WebHost.CreateDefaultBuilder()
.Configure(_ => { })
.Build();
var logger = host.Services.GetRequiredService<ILogger<WebHostTests>>();
logger.LogInformation("Request starting");
var events = listener.EventData.ToArray();
Assert.Contains(events, args =>
args.EventSource.Name == "Microsoft-Extensions-Logging" &&
args.Payload.OfType<string>().Any(p => p.Contains("Request starting")));
}
private class TestEventListener : EventListener
{
private volatile bool _disposed;
private ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();
public IEnumerable<EventWrittenEventArgs> EventData => _events;
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "Microsoft-Extensions-Logging")
{
EnableEvents(eventSource, EventLevel.Informational);
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (!_disposed)
{
_events.Enqueue(eventData);
}
}
public override void Dispose()
{
_disposed = true;
base.Dispose();
}
}
private class ReloadableMemorySource : IConfigurationSource
{
public IConfigurationProvider Build(IConfigurationBuilder builder)

0 comments on commit 4347a45

Please sign in to comment.