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

Commit 355b186

Browse files
authored
Logging in DI (#626)
1 parent 2a08aac commit 355b186

File tree

51 files changed

+1086
-1096
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1086
-1096
lines changed

samples/SampleApp/Program.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
76
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Logging;
99
using ILogger = Microsoft.Extensions.Logging.ILogger;
1010

@@ -23,27 +23,29 @@ public Program()
2323

2424
// A Web App based program would configure logging via the WebHostBuilder.
2525
// Create a logger factory with filters that can be applied across all logger providers.
26-
var factory = new LoggerFactory()
27-
.UseConfiguration(loggingConfiguration.GetSection("Logging"))
28-
.AddFilter(new Dictionary<string, LogLevel>
26+
var serviceCollection = new ServiceCollection()
27+
.AddLogging(builder =>
2928
{
30-
{ "Microsoft", LogLevel.Warning },
31-
{ "System", LogLevel.Warning },
32-
{ "SampleApp.Program", LogLevel.Debug }
33-
});
34-
35-
// providers may be added to a LoggerFactory before any loggers are created
29+
builder
30+
.AddConfiguration(loggingConfiguration.GetSection("Logging"))
31+
.AddFilter("Microsoft", LogLevel.Warning)
32+
.AddFilter("System", LogLevel.Warning)
33+
.AddFilter("SampleApp.Program", LogLevel.Debug)
34+
.AddConsole();
3635
#if NET46
37-
factory.AddEventLog();
36+
builder.AddEventLog();
3837
#elif NETCOREAPP2_0
3938
#else
4039
#error Target framework needs to be updated
4140
#endif
41+
});
42+
43+
// providers may be added to a LoggerFactory before any loggers are created
4244

43-
factory.AddConsole();
4445

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

4951
public static void Main(string[] args)

samples/SampleApp/SampleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<ItemGroup>
2020
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(AspNetCoreVersion)" />
2121
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
22+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
2223
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="$(AspNetCoreVersion)" />
2324
</ItemGroup>
2425

src/Microsoft.Extensions.Logging.Abstractions/ILoggerFactory.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public interface ILoggerFactory : IDisposable
1919
ILogger CreateLogger(string categoryName);
2020

2121
/// <summary>
22-
/// <para>
23-
/// 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.
24-
/// </para>
2522
/// Adds an <see cref="ILoggerProvider"/> to the logging system.
2623
/// </summary>
2724
/// <param name="provider">The <see cref="ILoggerProvider"/>.</param>
28-
[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.")]
2925
void AddProvider(ILoggerProvider provider);
3026
}
3127
}

src/Microsoft.Extensions.Logging.AzureAppServices/AzureAppServicesLoggerFactoryExtensions.cs

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

44
using System;
5+
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Logging.AzureAppServices;
67
using Microsoft.Extensions.Logging.AzureAppServices.Internal;
78

@@ -15,49 +16,41 @@ public static class AzureAppServicesLoggerFactoryExtensions
1516
/// <summary>
1617
/// Adds an Azure Web Apps diagnostics logger.
1718
/// </summary>
18-
/// <param name="factory">The extension method argument</param>
19-
public static LoggerFactory AddAzureWebAppDiagnostics(this LoggerFactory factory)
19+
/// <param name="builder">The extension method argument</param>
20+
public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder)
2021
{
21-
return AddAzureWebAppDiagnostics(factory, new AzureAppServicesDiagnosticsSettings());
22+
return AddAzureWebAppDiagnostics(builder, null);
2223
}
2324

2425
/// <summary>
2526
/// Adds an Azure Web Apps diagnostics logger.
2627
/// </summary>
27-
/// <param name="factory">The extension method argument</param>
28+
/// <param name="builder">The extension method argument</param>
2829
/// <param name="settings">The setting object to configure loggers.</param>
29-
public static LoggerFactory AddAzureWebAppDiagnostics(this LoggerFactory factory, AzureAppServicesDiagnosticsSettings settings)
30+
public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, AzureAppServicesDiagnosticsSettings settings)
3031
{
3132
if (WebAppContext.Default.IsRunningInAzureWebApp)
3233
{
3334
// Only add the provider if we're in Azure WebApp. That cannot change once the apps started
34-
factory.AddProvider("AzureAppServices", new AzureAppServicesDiagnosticsLoggerProvider(WebAppContext.Default, settings));
35+
builder.Services.AddSingleton<ILoggerProvider>(new AzureAppServicesDiagnosticsLoggerProvider(WebAppContext.Default, settings ?? new AzureAppServicesDiagnosticsSettings()));
3536
}
36-
return factory;
37+
return builder;
3738
}
3839

3940
/// <summary>
40-
/// <para>
41-
/// 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.
42-
/// </para>
4341
/// Adds an Azure Web Apps diagnostics logger.
4442
/// </summary>
4543
/// <param name="factory">The extension method argument</param>
46-
[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.")]
4744
public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory)
4845
{
4946
return AddAzureWebAppDiagnostics(factory, new AzureAppServicesDiagnosticsSettings());
5047
}
5148

5249
/// <summary>
53-
/// <para>
54-
/// 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.
55-
/// </para>
5650
/// Adds an Azure Web Apps diagnostics logger.
5751
/// </summary>
5852
/// <param name="factory">The extension method argument</param>
5953
/// <param name="settings">The setting object to configure loggers.</param>
60-
[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.")]
6154
public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory, AzureAppServicesDiagnosticsSettings settings)
6255
{
6356
if (WebAppContext.Default.IsRunningInAzureWebApp)

src/Microsoft.Extensions.Logging.AzureAppServices/Internal/AzureAppServicesDiagnosticsLoggerProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.Extensions.Logging.AzureAppServices.Internal
1010
/// <summary>
1111
/// Logger provider for Azure WebApp.
1212
/// </summary>
13+
[ProviderAlias("AzureAppServices")]
1314
public class AzureAppServicesDiagnosticsLoggerProvider : ILoggerProvider
1415
{
1516
private readonly IWebAppLogConfigurationReader _configurationReader;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Options;
6+
7+
namespace Microsoft.Extensions.Logging.Console
8+
{
9+
public class ConfigurationConsoleLoggerConfigureOptions : ConfigureOptions<ConsoleLoggerOptions>
10+
{
11+
public ConfigurationConsoleLoggerConfigureOptions(IConfiguration configuration) : base(configuration.Bind)
12+
{
13+
}
14+
}
15+
}

src/Microsoft.Extensions.Logging.Console/ConsoleLoggerFactoryExtensions.cs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Logging.Console;
78

89
namespace Microsoft.Extensions.Logging
@@ -12,68 +13,89 @@ public static class ConsoleLoggerExtensions
1213
/// <summary>
1314
/// Adds a console logger named 'Console' to the factory.
1415
/// </summary>
15-
/// <param name="factory">The <see cref="LoggerFactory"/> to use.</param>
16-
public static LoggerFactory AddConsole(this LoggerFactory factory)
16+
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
17+
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder)
1718
{
18-
factory.AddProvider("Console", new ConsoleLoggerProvider(factory.Configuration));
19-
return factory;
19+
builder.Services.AddSingleton<ILoggerProvider, ConsoleLoggerProvider>();
20+
21+
return builder;
22+
}
23+
24+
/// <summary>
25+
/// Adds a console logger named 'Console' to the factory.
26+
/// </summary>
27+
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
28+
/// <param name="configure"></param>
29+
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder, Action<ConsoleLoggerOptions> configure)
30+
{
31+
if (configure == null)
32+
{
33+
throw new ArgumentNullException(nameof(configure));
34+
}
35+
36+
builder.AddConsole();
37+
builder.Services.Configure(configure);
38+
39+
return builder;
40+
}
41+
42+
/// <summary>
43+
/// Adds a console logger named 'Console' to the factory.
44+
/// </summary>
45+
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
46+
/// <param name="configuration"></param>
47+
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder, IConfiguration configuration)
48+
{
49+
if (configuration == null)
50+
{
51+
throw new ArgumentNullException(nameof(configuration));
52+
}
53+
54+
builder.AddConsole();
55+
builder.Services.Configure<ConsoleLoggerOptions>(configuration);
56+
57+
return builder;
2058
}
2159

2260
/// <summary>
23-
/// <para>
24-
/// 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.
25-
/// </para>
2661
/// Adds a console logger that is enabled for <see cref="LogLevel"/>.Information or higher.
2762
/// </summary>
2863
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
29-
[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.")]
3064
public static ILoggerFactory AddConsole(this ILoggerFactory factory)
3165
{
3266
return factory.AddConsole(includeScopes: false);
3367
}
3468

3569
/// <summary>
36-
/// <para>
37-
/// 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.
38-
/// </para>
3970
/// Adds a console logger that is enabled for <see cref="LogLevel"/>.Information or higher.
4071
/// </summary>
4172
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
4273
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
4374
/// in the output.</param>
44-
[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.")]
4575
public static ILoggerFactory AddConsole(this ILoggerFactory factory, bool includeScopes)
4676
{
4777
factory.AddConsole((n, l) => l >= LogLevel.Information, includeScopes);
4878
return factory;
4979
}
5080

5181
/// <summary>
52-
/// <para>
53-
/// 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.
54-
/// </para>
5582
/// Adds a console logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher.
5683
/// </summary>
5784
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
5885
/// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param>
59-
[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.")]
6086
public static ILoggerFactory AddConsole(this ILoggerFactory factory, LogLevel minLevel)
6187
{
6288
factory.AddConsole(minLevel, includeScopes: false);
6389
return factory;
6490
}
6591

6692
/// <summary>
67-
/// <para>
68-
/// 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.
69-
/// </para>
7093
/// Adds a console logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher.
7194
/// </summary>
7295
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
7396
/// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param>
7497
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
7598
/// in the output.</param>
76-
[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.")]
7799
public static ILoggerFactory AddConsole(
78100
this ILoggerFactory factory,
79101
LogLevel minLevel,
@@ -84,14 +106,10 @@ public static ILoggerFactory AddConsole(
84106
}
85107

86108
/// <summary>
87-
/// <para>
88-
/// 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.
89-
/// </para>
90109
/// Adds a console logger that is enabled as defined by the filter function.
91110
/// </summary>
92111
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
93112
/// <param name="filter">The category filter to apply to logs.</param>
94-
[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.")]
95113
public static ILoggerFactory AddConsole(
96114
this ILoggerFactory factory,
97115
Func<string, LogLevel, bool> filter)
@@ -101,16 +119,12 @@ public static ILoggerFactory AddConsole(
101119
}
102120

103121
/// <summary>
104-
/// <para>
105-
/// 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.
106-
/// </para>
107122
/// Adds a console logger that is enabled as defined by the filter function.
108123
/// </summary>
109124
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
110125
/// <param name="filter">The category filter to apply to logs.</param>
111126
/// <param name="includeScopes">A value which indicates whether log scope information should be displayed
112127
/// in the output.</param>
113-
[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.")]
114128
public static ILoggerFactory AddConsole(
115129
this ILoggerFactory factory,
116130
Func<string, LogLevel, bool> filter,
@@ -122,14 +136,10 @@ public static ILoggerFactory AddConsole(
122136

123137

124138
/// <summary>
125-
/// <para>
126-
/// 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.
127-
/// </para>
128139
/// </summary>
129140
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
130141
/// <param name="settings">The settings to apply to created <see cref="ConsoleLogger"/>'s.</param>
131142
/// <returns></returns>
132-
[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.")]
133143
public static ILoggerFactory AddConsole(
134144
this ILoggerFactory factory,
135145
IConsoleLoggerSettings settings)
@@ -139,14 +149,10 @@ public static ILoggerFactory AddConsole(
139149
}
140150

141151
/// <summary>
142-
/// <para>
143-
/// 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.
144-
/// </para>
145152
/// </summary>
146153
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
147154
/// <param name="configuration">The <see cref="IConfiguration"/> to use for <see cref="IConsoleLoggerSettings"/>.</param>
148155
/// <returns></returns>
149-
[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.")]
150156
public static ILoggerFactory AddConsole(this ILoggerFactory factory, IConfiguration configuration)
151157
{
152158
var settings = new ConfigurationConsoleLoggerSettings(configuration);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.Extensions.Logging.Console
5+
{
6+
public class ConsoleLoggerOptions
7+
{
8+
public bool IncludeScopes { get; set; } = false;
9+
}
10+
}

0 commit comments

Comments
 (0)