-
Notifications
You must be signed in to change notification settings - Fork 165
/
AspNetExtensions.cs
142 lines (120 loc) · 4.87 KB
/
AspNetExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Web.Internal;
using NLog.Extensions.Logging;
using NLog.Web.AspNetCore;
#if NETSTANDARD2_0
using Microsoft.Extensions.DependencyInjection;
#endif
namespace NLog.Web
{
/// <summary>
/// Helpers for ASP.NET
/// </summary>
public static class AspNetExtensions
{
/// <summary>
/// Enable NLog Web for ASP.NET Core.
/// </summary>
/// <param name="app"></param>
#if NETSTANDARD2_0
[Obsolete("Use UseNLog() on IWebHostBuilder")]
#endif
public static void AddNLogWeb(this IApplicationBuilder app)
{
ServiceLocator.ServiceProvider = app.ApplicationServices;
}
/// <summary>
/// Apply NLog configuration from XML config.
/// </summary>
/// <param name="env"></param>
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
/// <returns>LoggingConfiguration for chaining</returns>
public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, string configFileRelativePath)
{
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
return ConfigureNLog(fileName);
}
/// <summary>
/// Apply NLog configuration from XML config.
/// </summary>
/// <param name="fileName">Path to NLog configuration file, e.g. nlog.config. </param>
/// <returns>LoggingConfiguration for chaining</returns>
private static LoggingConfiguration ConfigureNLog(string fileName)
{
var configuration = new XmlLoggingConfiguration(fileName, true);
LogManager.Configuration = configuration;
return configuration;
}
#if NETSTANDARD2_0
/// <summary>
/// Apply NLog configuration from XML config.
///
/// This call is not needed when <see cref="NLogBuilder.ConfigureNLog(string)"/> is used.
/// </summary>
/// <param name="builder">The logging builder</param>
/// <param name="configFileName">Path to NLog configuration file, e.g. nlog.config. </param>>
/// <returns>LogFactory to get loggers, add events etc</returns>
public static LogFactory ConfigureNLog(this ILoggingBuilder builder, string configFileName)
{
return builder.ConfigureNLog(new XmlLoggingConfiguration(configFileName));
}
/// <summary>
/// Configure NLog from API
///
/// This call is not needed when <see cref="NLogBuilder.ConfigureNLog(LoggingConfiguration)"/> is used.
/// </summary>
/// <param name="builder">The logging builder</param>
/// <param name="configuration">Config for NLog</param>
/// <returns>LogFactory to get loggers, add events etc</returns>
public static LogFactory ConfigureNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
//ConfigureHiddenAssemblies
//todo rename there to ConfigureNLog?
builder.AddNLog();
return NLogBuilder.ConfigureNLog(configuration);
}
/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
public static IWebHostBuilder UseNLog(this IWebHostBuilder builder)
{
return UseNLog(builder, null);
}
/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
/// <param name="builder"></param>
/// <param name="options">Options for logging to NLog with Dependency Injected loggers</param>
/// <returns></returns>
public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCoreOptions options)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
options = options ?? NLogAspNetCoreOptions.Default;
builder.ConfigureServices(services =>
{
services.AddSingleton<ILoggerFactory>(serviceProvider =>
{
ServiceLocator.ServiceProvider = serviceProvider;
NLogBuilder.RegisterNLogWebAspNetCore();
LogManager.Configuration?.Reload();
return new NLogLoggerFactory(options);
});
//note: this one is called before services.AddSingleton<ILoggerFactory>
if (options.RegisterHttpContextAccessor)
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
});
return builder;
}
#endif
}
}