From c0b6c1af730364cd6d3a7ee9c79704aaf28b9b40 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sun, 27 Oct 2019 17:35:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LogStudy.WebApp/Controllers/LogController.cs | 54 +++++++++++++ .../Controllers/ValuesController.cs | 4 - LogStudy.WebApp/LogStudy.WebApp.csproj | 4 + LogStudy.WebApp/Program.cs | 77 +++++++++++++++---- LogStudy.WebApp/Startup.cs | 34 +++++++- LogStudy.WebApp/appsettings.Development.json | 4 +- LogStudy.WebApp/appsettings.json | 19 ++++- 7 files changed, 170 insertions(+), 26 deletions(-) create mode 100644 LogStudy.WebApp/Controllers/LogController.cs diff --git a/LogStudy.WebApp/Controllers/LogController.cs b/LogStudy.WebApp/Controllers/LogController.cs new file mode 100644 index 0000000..9b7f2e7 --- /dev/null +++ b/LogStudy.WebApp/Controllers/LogController.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace LogStudy.WebApp.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class LogController : ControllerBase + { + private ILogger logger; + public LogController(ILogger logger) + { + this.logger = logger; + } + + [HttpGet] + [HttpPost] + public IActionResult Index() + { + logger.LogTrace("EventSource 提供程序"); + logger.LogError(new EventId(222, "EWT测试"), "EWT错误{id}", 222); + logger.LogDebug("LogDebug"); + logger.LogInformation("LogInformation"); + logger.LogWarning("LogWarning"); + logger.LogError("LogError"); + logger.LogCritical("LogCritical"); + + return new JsonResult(new { Code=0, Message="日志记录成功" }); + } + + [HttpGet] + [HttpPost] + public IActionResult NoDI() + { + var loggerFactory = new LoggerFactory().AddConsole(); + var logger2 = loggerFactory.CreateLogger(); + + logger2.LogTrace("LogTrace"); + logger2.LogDebug("LogDebug"); + logger2.LogInformation("LogInformation"); + logger2.LogWarning("LogWarning"); + logger2.LogError("LogError"); + logger2.LogCritical("LogCritical"); + + + return new JsonResult(new { Code = 0, Message = "日志记录成功" }); + } + } +} \ No newline at end of file diff --git a/LogStudy.WebApp/Controllers/ValuesController.cs b/LogStudy.WebApp/Controllers/ValuesController.cs index b54bca1..125a072 100644 --- a/LogStudy.WebApp/Controllers/ValuesController.cs +++ b/LogStudy.WebApp/Controllers/ValuesController.cs @@ -23,10 +23,6 @@ namespace LogStudy.WebApp.Controllers [HttpGet] public ActionResult> Get() { - _logger.LogCritical("我是测试"); - - _logger.LogDebug("我是Debug测试"); - return new string[] { "value1", "value2" }; } diff --git a/LogStudy.WebApp/LogStudy.WebApp.csproj b/LogStudy.WebApp/LogStudy.WebApp.csproj index fe62861..71ab075 100644 --- a/LogStudy.WebApp/LogStudy.WebApp.csproj +++ b/LogStudy.WebApp/LogStudy.WebApp.csproj @@ -9,11 +9,15 @@ + + + + diff --git a/LogStudy.WebApp/Program.cs b/LogStudy.WebApp/Program.cs index 876bec1..1226303 100644 --- a/LogStudy.WebApp/Program.cs +++ b/LogStudy.WebApp/Program.cs @@ -6,7 +6,10 @@ using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Logging.Debug; namespace LogStudy.WebApp { @@ -14,23 +17,65 @@ namespace LogStudy.WebApp { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + var host = CreateWebHostBuilder(args).Build(); + + //启动检查:使用日志 + var logger = host.Services.GetRequiredService>(); + logger.LogInformation("检查数据库连接"); + logger.LogDebug(new EventId(1000,"事件名"),"检查缓存{id}",222); + //少提供一个参数 + logger.LogInformation("1001","检查MQ{id}{name}",3333); + + host.Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .ConfigureLogging((hostbuildContext, logBuild) => { - var config = hostbuildContext.Configuration; - var ev = hostbuildContext.HostingEnvironment; - - logBuild - .AddConsole() - .AddDebug() - .AddEventLog() - .AddEventSourceLogger() - .AddTraceSource("NetCoreDemo") - .SetMinimumLevel(LogLevel.Information); - }) - .UseStartup(); + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + { + var webBuilder = WebHost.CreateDefaultBuilder(args) + //配置日志 + .ConfigureLogging((hostbuildContext, logBuild) => + { + //CreateDefaultBuilder 默认注册了日志组件: + //使用appsettings.环境变量.json 的Logging配置节 + //默认添加了以下提供程序:Console、 Debug、EventSource + //详情可以查看源代码 + + //本方法ConfigureLogging,重新配置日志 + //配置信息 + var config = hostbuildContext.Configuration; + //环境变量 + var ev = hostbuildContext.HostingEnvironment; + //注入服务 + var service=logBuild.Services; + + logBuild + //自定义配置信息 + .AddConfiguration(config.GetSection("Logging")) + //添加Console提供程序 + .AddConsole() + //添加Debug提供程序 + .AddDebug() + //添加EventLog提供程序 + .AddEventLog() + //添加EventSourceLogger提供程序:EWT + .AddEventSourceLogger() + //添加TraceSource提供程序 + .AddTraceSource("NetCoreDemo") + //设置最小默认日志级别 + .SetMinimumLevel(LogLevel.Information) + //添加筛选器:会覆盖上面配置信息 + .AddFilter("System", LogLevel.Debug) //不指定提供程序,则应用与全部 + .AddFilter("Microsoft", LogLevel.Trace);//只作用于指定的提供程序 DebugLoggerProvider + + //以下方式,可以使用第三方日志组件,如Nlog、Log4net等 + //.ClearProviders() + //.AddProvider(null) + + }) + .UseStartup(); + + return webBuilder; + } + } } diff --git a/LogStudy.WebApp/Startup.cs b/LogStudy.WebApp/Startup.cs index c8845ee..696ed87 100644 --- a/LogStudy.WebApp/Startup.cs +++ b/LogStudy.WebApp/Startup.cs @@ -9,14 +9,34 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Logging.Configuration; +using Microsoft.Extensions.Logging.Console; +using System.IO; namespace LogStudy.WebApp { + /// + /// 使用日志: + /// 1、构造函数注入 + /// 2、使用 LoggerFactory 来创建 ILogger + /// public class Startup { - public Startup(IConfiguration configuration) + /// + /// 构建函数注入 + /// + private readonly ILogger _logger; + + /// + /// 工厂方法创建 + /// + private readonly ILogger _logger2; + public Startup(IConfiguration configuration,ILogger logger) { Configuration = configuration; + _logger=logger; + _logger2=GetLoggerFromFactory("Startup"); } public IConfiguration Configuration { get; } @@ -24,7 +44,10 @@ namespace LogStudy.WebApp // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + _logger.LogInformation("配置服务开始"); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + + _logger2.LogInformation("配置服务结束"); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -37,5 +60,14 @@ namespace LogStudy.WebApp app.UseMvc(); } + + private ILogger GetLoggerFromFactory(string name) + { + var fac=new LoggerFactory(); + + var logger = fac.AddConsole().CreateLogger(name); + + return logger; + } } } diff --git a/LogStudy.WebApp/appsettings.Development.json b/LogStudy.WebApp/appsettings.Development.json index e203e94..331805c 100644 --- a/LogStudy.WebApp/appsettings.Development.json +++ b/LogStudy.WebApp/appsettings.Development.json @@ -2,8 +2,8 @@ "Logging": { "LogLevel": { "Default": "Debug", - "System": "Information", - "Microsoft": "Information" + "System": "Debug", + "Microsoft": "Debug" } } } diff --git a/LogStudy.WebApp/appsettings.json b/LogStudy.WebApp/appsettings.json index def9159..8607097 100644 --- a/LogStudy.WebApp/appsettings.json +++ b/LogStudy.WebApp/appsettings.json @@ -1,7 +1,20 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" + "Logging": + { + "LogLevel": + { + "Default": "Information", + "System": "Information", + "Microsoft": "Information" + }, + "Console": + { + "IncludeScopes": true, + "LogLevel": { + "Default": "Debug", + "System": "Debug", + "Microsoft": "Debug" + } } }, "AllowedHosts": "*"