From 82ffcb6f553fd81b7027c1fa92976c347ae4cd65 Mon Sep 17 00:00:00 2001 From: "454313500@qq.com" <454313500@qq.com> Date: Tue, 23 Mar 2021 13:58:44 +0800 Subject: [PATCH] v1.0.5 --- CC.Yi.API/CC.Yi.API.csproj | 8 +++- CC.Yi.API/Controllers/StudentController.cs | 8 +++- CC.Yi.API/Program.cs | 46 +++++++++++++++------- CC.Yi.API/nlog.config | 35 ++++++++++++++++ CC.Yi.Common/CC.Yi.Common.csproj | 1 + 5 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 CC.Yi.API/nlog.config diff --git a/CC.Yi.API/CC.Yi.API.csproj b/CC.Yi.API/CC.Yi.API.csproj index 697c4d89..25450b8f 100644 --- a/CC.Yi.API/CC.Yi.API.csproj +++ b/CC.Yi.API/CC.Yi.API.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -21,4 +21,10 @@ + + + Always + + + diff --git a/CC.Yi.API/Controllers/StudentController.cs b/CC.Yi.API/Controllers/StudentController.cs index 66fc4c0b..eaf61812 100644 --- a/CC.Yi.API/Controllers/StudentController.cs +++ b/CC.Yi.API/Controllers/StudentController.cs @@ -19,6 +19,7 @@ namespace CC.Yi.API.Controllers { _studentBll = studentBll; _logger = logger; + _logger.LogInformation("现在你进入了StudentController控制器"); } @@ -29,13 +30,15 @@ namespace CC.Yi.API.Controllers [HttpGet] public IActionResult GetTest()//查 { + _logger.LogInformation("调用查方法"); var data = _studentBll.GetAllEntities().ToList(); return Content(Common.JsonHelper.JsonToString(data)); } [HttpGet] public IActionResult AddTest()//增 { - List students = new List() {new student { name = "学生a" } ,new student { name="学生d"} }; + _logger.LogInformation("调用增方法"); + List students = new List() {new student { name = "学生a" } ,new student { name="学生d"} }; _studentBll.Add(students); return Content("ok"); @@ -43,7 +46,7 @@ namespace CC.Yi.API.Controllers [HttpGet] public IActionResult RemoveTest()//删 { - + _logger.LogInformation("调用删方法"); if (_studentBll.Delete(u=>u.name=="学生a")) { return Content("ok"); @@ -56,6 +59,7 @@ namespace CC.Yi.API.Controllers [HttpGet] public IActionResult UpdateTest()//改 { + _logger.LogInformation("调用改方法"); if (_studentBll.Update(new student { id=2, name = "学生a" }, "name")) { return Content("ok"); diff --git a/CC.Yi.API/Program.cs b/CC.Yi.API/Program.cs index f10b5838..83fc65fc 100644 --- a/CC.Yi.API/Program.cs +++ b/CC.Yi.API/Program.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using NLog.Web; using System; using System.Collections.Generic; using System.Linq; @@ -16,22 +17,33 @@ namespace CC.Yi.API { public static void Main(string[] args) { - var host = CreateHostBuilder(args).Build(); - var scope = host.Services.CreateScope(); + var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + try { + logger.Debug("Yiܡ"); + var host = CreateHostBuilder(args).Build(); + var scope = host.Services.CreateScope(); var services = scope.ServiceProvider; - try - { - var context = services.GetRequiredService(); - DbContentFactory.Initialize(context); - } - catch (Exception ex) - { - var logger = services.GetRequiredService>(); - logger.LogError(ex, "An error occurred while seeding the database."); - } + var context = services.GetRequiredService();//ȡ + DbContentFactory.Initialize(context);//þ̬෽ע + host.Run(); + logger.Info("Yiɹ"); } - host.Run(); + catch (Exception exception) + { + //NLog: catch setup errors + logger.Error(exception, "Stopped program because of exception"); + throw; + } + finally + { + // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) + NLog.LogManager.Shutdown(); + } + + + + } public static IHostBuilder CreateHostBuilder(string[] args) => @@ -39,6 +51,12 @@ namespace CC.Yi.API .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); - }).UseServiceProviderFactory(new AutofacServiceProviderFactory()); + }).UseServiceProviderFactory(new AutofacServiceProviderFactory()) + .ConfigureLogging(logging => + { + // logging.ClearProviders(); // п̨ + logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + }).UseNLog(); + } } diff --git a/CC.Yi.API/nlog.config b/CC.Yi.API/nlog.config new file mode 100644 index 00000000..ff359770 --- /dev/null +++ b/CC.Yi.API/nlog.config @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CC.Yi.Common/CC.Yi.Common.csproj b/CC.Yi.Common/CC.Yi.Common.csproj index 3e6d258d..c1361bd4 100644 --- a/CC.Yi.Common/CC.Yi.Common.csproj +++ b/CC.Yi.Common/CC.Yi.Common.csproj @@ -9,6 +9,7 @@ +