.NET CORE 日志系统
作者:互联网
1.ILogger .NET CORE内置的日志服务
需要引入的NuGet包:Microsoft.Extensions.Logging、Microsoft.Extensions.Logging.Console、Microsoft.Extensions.DependencyInjection
public class TestCon
{
private readonly ILogger<TestCon> logger;
public TestCon(ILogger<TestCon> logger)
{
this.logger = logger;
}
public void Test()
{
logger.LogDebug("sssssssssss");
logger.LogError("ssssssss");
try
{
File.ReadAllText("c:/333.txt");
}
catch (Exception EX)
{
logger.LogError(EX, "读取文件失败");
}
}
}
//Program中调用:
ServiceCollection services = new ServiceCollection();
services.AddLogging(builder => {
builder.AddConsole();
});
services.AddScoped<TestCon>();
using(var scope = services.BuildServiceProvider())
{
var SS= scope.GetService<TestCon>();
SS.Test();
}
2.NLOG的使用:
需要的NuGet包:NLog.Extensions.Logging
配置文件默认的名称为:nlog.config 配置文件可在官网下载
services.AddLogging(builder => {
builder.AddConsole();
builder.AddNLog();
});
//调用方式不变
3.NLOG配置
//archiveAboveSize 单文件最大多少字节 maxArchiveFiles:最多几个文件
//SystemServices下的类使用sysServiceFile这个target进行记录 最小级别是Trance final:如果匹配到这项规则就终止
标签:CORE,Logging,builder,public,Extensions,services,NET,logger,日志 来源: https://www.cnblogs.com/wilsonNet/p/16427764.html