This commit is contained in:
454313500@qq.com
2021-03-23 13:58:44 +08:00
parent da68f9255d
commit 82ffcb6f55
5 changed files with 81 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
@@ -21,4 +21,10 @@
<ProjectReference Include="..\CC.Yi.Model\CC.Yi.Model.csproj" />
</ItemGroup>
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -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<student> students = new List<student>() {new student { name = "学生a" } ,new student { name="学生d"} };
_logger.LogInformation("调用增方法");
List<student> students = new List<student>() {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");

View File

@@ -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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Yi<59><69><EFBFBD><EFBFBD><EFBFBD>ܡ<EFBFBD><DCA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
var host = CreateHostBuilder(args).Build();
var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<Model.DataContext>();
DbContentFactory.Initialize(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
var context = services.GetRequiredService<Model.DataContext>();//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
host.Run();
logger.Info("Yi<59><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>");
}
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<Startup>();
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());
}).UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureLogging(logging =>
{
// logging.ClearProviders(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
}).UseNLog();
}
}

35
CC.Yi.API/nlog.config Normal file
View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="false"
internalLogLevel="Warn"
internalLogFile="Logs\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<!-- 写入文件配置 -->
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="Logs\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring} ${newline}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="Logs\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action} ${newline}" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<!--minlevel 改为Trace 跟踪全部 Error 只捕获异常-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>

View File

@@ -9,6 +9,7 @@
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.11.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>