ارتقاء به ASP.NET Core 1.0 - قسمت 17 - بررسی فریم ورک Logging
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۵/۰۱ ۱۴:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); {
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
} {
"dependencies": {
//same as before
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0"
}
}
[Route("[controller]")]
public class AboutController : Controller
{
private readonly ILogger<AboutController> _logger;
public AboutController(ILogger<AboutController> logger)
{
_logger = logger;
}
[Route("")]
public ActionResult Hello()
{
_logger.LogInformation("Running Hello");
return Content("Hello from DNT!");
}
loggerFactory.AddDebug(minLevel: LogLevel.Information);
public enum LogLevel
{
Trace,
Debug,
Information,
Warning,
Error,
Critical,
None,
} <Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
</ItemGroup>
</Project> _logger.LogInformation("LogInformation"); logging.AddDbLogger(); // You can change its Log Level using the `appsettings.json` file -> Logging -> LogLevel -> Default
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddMvc();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.Build(); {
"Logging": {
"IncludeScopes": false,
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
} > dotnet add package Microsoft.Windows.Compatibility --version 2.0.0-preview1-25914-04
var webHost = new WebHostBuilder()
//...
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventLog();
})
//... {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": { // here ...
"LogLevel": {
"Default": "Information" //Trace, Debug
}
}
}
} {
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddDebug();
if (hostingContext.HostingEnvironment.IsDevelopment())
{
logging.AddConsole();
}
})
.UseStartup<Startup>(); Console Debug EventSource
using Serilog;
using Serilog.Exceptions;
ILogger logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails() // This Line
.WriteTo.RollingFile(
new JsonFormatter(renderMessage: true),
@"C:\logs\log-{Date}.txt")
.CreateLogger(); // we want to prevent a circular dependency between ILoggerFactory and CustomLoggers private ILoggerFactory? _loggerFactory; private ILoggerFactory LoggerFactory => _loggerFactory ??= serviceProvider.GetRequiredService<ILoggerFactory>();