Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Logging in DI #626

Merged
merged 20 commits into from
Jun 2, 2017
Merged
28 changes: 15 additions & 13 deletions samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;

Expand All @@ -23,27 +23,29 @@ public Program()

// A Web App based program would configure logging via the WebHostBuilder.
// Create a logger factory with filters that can be applied across all logger providers.
var factory = new LoggerFactory()
.UseConfiguration(loggingConfiguration.GetSection("Logging"))
.AddFilter(new Dictionary<string, LogLevel>
var serviceCollection = new ServiceCollection()
.AddLogging(builder =>
{
{ "Microsoft", LogLevel.Warning },
{ "System", LogLevel.Warning },
{ "SampleApp.Program", LogLevel.Debug }
});

// providers may be added to a LoggerFactory before any loggers are created
builder
.AddConfiguration(loggingConfiguration.GetSection("Logging"))
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("SampleApp.Program", LogLevel.Debug)
.AddConsole();
#if NET46
factory.AddEventLog();
builder.AddEventLog();
#elif NETCOREAPP2_0
#else
#error Target framework needs to be updated
#endif
});

// providers may be added to a LoggerFactory before any loggers are created

factory.AddConsole();

var serviceProvider = serviceCollection.BuildServiceProvider();
// getting the logger using the class's name is conventional
_logger = factory.CreateLogger<Program>();
_logger = serviceProvider.GetRequiredService<ILogger<Program>>();
}

public static void Main(string[] args)
Expand Down
1 change: 1 addition & 0 deletions samples/SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="$(AspNetCoreVersion)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ public interface ILoggerFactory : IDisposable
ILogger CreateLogger(string categoryName);

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddProvider() method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds an <see cref="ILoggerProvider"/> to the logging system.
/// </summary>
/// <param name="provider">The <see cref="ILoggerProvider"/>.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddProvider() method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we decide to un-obsolete everything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, EF uses AddProvider and there is no time to fix it there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete doesn't mean it wont work, calling AddProvider should still be fine they just have to ignore the warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issues is there is no "right" way for them to do it right now, so showing obsolete warning is not entirely correct.

void AddProvider(ILoggerProvider provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.AzureAppServices;
using Microsoft.Extensions.Logging.AzureAppServices.Internal;

Expand All @@ -15,49 +16,41 @@ public static class AzureAppServicesLoggerFactoryExtensions
/// <summary>
/// Adds an Azure Web Apps diagnostics logger.
/// </summary>
/// <param name="factory">The extension method argument</param>
public static LoggerFactory AddAzureWebAppDiagnostics(this LoggerFactory factory)
/// <param name="builder">The extension method argument</param>
public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder)
{
return AddAzureWebAppDiagnostics(factory, new AzureAppServicesDiagnosticsSettings());
return AddAzureWebAppDiagnostics(builder, null);
}

/// <summary>
/// Adds an Azure Web Apps diagnostics logger.
/// </summary>
/// <param name="factory">The extension method argument</param>
/// <param name="builder">The extension method argument</param>
/// <param name="settings">The setting object to configure loggers.</param>
public static LoggerFactory AddAzureWebAppDiagnostics(this LoggerFactory factory, AzureAppServicesDiagnosticsSettings settings)
public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, AzureAppServicesDiagnosticsSettings settings)
{
if (WebAppContext.Default.IsRunningInAzureWebApp)
{
// Only add the provider if we're in Azure WebApp. That cannot change once the apps started
factory.AddProvider("AzureAppServices", new AzureAppServicesDiagnosticsLoggerProvider(WebAppContext.Default, settings));
builder.Services.AddSingleton<ILoggerProvider>(new AzureAppServicesDiagnosticsLoggerProvider(WebAppContext.Default, settings ?? new AzureAppServicesDiagnosticsSettings()));
}
return factory;
return builder;
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddAzureWebAppDiagnostics() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds an Azure Web Apps diagnostics logger.
/// </summary>
/// <param name="factory">The extension method argument</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddAzureWebAppDiagnostics() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory)
{
return AddAzureWebAppDiagnostics(factory, new AzureAppServicesDiagnosticsSettings());
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddAzureWebAppDiagnostics() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds an Azure Web Apps diagnostics logger.
/// </summary>
/// <param name="factory">The extension method argument</param>
/// <param name="settings">The setting object to configure loggers.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddAzureWebAppDiagnostics() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory, AzureAppServicesDiagnosticsSettings settings)
{
if (WebAppContext.Default.IsRunningInAzureWebApp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Extensions.Logging.AzureAppServices.Internal
/// <summary>
/// Logger provider for Azure WebApp.
/// </summary>
[ProviderAlias("AzureAppServices")]
public class AzureAppServicesDiagnosticsLoggerProvider : ILoggerProvider
{
private readonly IWebAppLogConfigurationReader _configurationReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Logging.Console
{
public class ConfigurationConsoleLoggerConfigureOptions : ConfigureOptions<ConsoleLoggerOptions>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit much?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need suggestions :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigureConsole[Logger]Options?

{
public ConfigurationConsoleLoggerConfigureOptions(IConfiguration configuration) : base(configuration.Bind)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Console;

namespace Microsoft.Extensions.Logging
Expand All @@ -12,68 +13,89 @@ public static class ConsoleLoggerExtensions
/// <summary>
/// Adds a console logger named 'Console' to the factory.
/// </summary>
/// <param name="factory">The <see cref="LoggerFactory"/> to use.</param>
public static LoggerFactory AddConsole(this LoggerFactory factory)
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder)
{
factory.AddProvider("Console", new ConsoleLoggerProvider(factory.Configuration));
return factory;
builder.Services.AddSingleton<ILoggerProvider, ConsoleLoggerProvider>();

return builder;
}

/// <summary>
/// Adds a console logger named 'Console' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="configure"></param>
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder, Action<ConsoleLoggerOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.AddConsole();
builder.Services.Configure(configure);

return builder;
}

/// <summary>
/// Adds a console logger named 'Console' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="configuration"></param>
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder, IConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

builder.AddConsole();
builder.Services.Configure<ConsoleLoggerOptions>(configuration);

return builder;
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled for <see cref="LogLevel"/>.Information or higher.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(this ILoggerFactory factory)
{
return factory.AddConsole(includeScopes: false);
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled for <see cref="LogLevel"/>.Information or higher.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
/// in the output.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(this ILoggerFactory factory, bool includeScopes)
{
factory.AddConsole((n, l) => l >= LogLevel.Information, includeScopes);
return factory;
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(this ILoggerFactory factory, LogLevel minLevel)
{
factory.AddConsole(minLevel, includeScopes: false);
return factory;
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param>
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
/// in the output.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(
this ILoggerFactory factory,
LogLevel minLevel,
Expand All @@ -84,14 +106,10 @@ public static ILoggerFactory AddConsole(
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled as defined by the filter function.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="filter">The category filter to apply to logs.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(
this ILoggerFactory factory,
Func<string, LogLevel, bool> filter)
Expand All @@ -101,16 +119,12 @@ public static ILoggerFactory AddConsole(
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// Adds a console logger that is enabled as defined by the filter function.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="filter">The category filter to apply to logs.</param>
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
/// in the output.</param>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(
this ILoggerFactory factory,
Func<string, LogLevel, bool> filter,
Expand All @@ -122,14 +136,10 @@ public static ILoggerFactory AddConsole(


/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="settings">The settings to apply to created <see cref="ConsoleLogger"/>'s.</param>
/// <returns></returns>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(
this ILoggerFactory factory,
IConsoleLoggerSettings settings)
Expand All @@ -139,14 +149,10 @@ public static ILoggerFactory AddConsole(
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.
/// </para>
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> to use for <see cref="IConsoleLoggerSettings"/>.</param>
/// <returns></returns>
[Obsolete("This method is obsolete and will be removed in a future version. The recommended alternative is to call the Microsoft.Extensions.Logging.AddConsole() extension method on the Microsoft.Extensions.Logging.LoggerFactory instance.")]
public static ILoggerFactory AddConsole(this ILoggerFactory factory, IConfiguration configuration)
{
var settings = new ConfigurationConsoleLoggerSettings(configuration);
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.Extensions.Logging.Console/ConsoleLoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Extensions.Logging.Console
{
public class ConsoleLoggerOptions
{
public bool IncludeScopes { get; set; } = false;
}
}
Loading