本文主要内容为.NET Core的日志记录程序和常使用的日志记录框架的简单使用
首先,打开VS2019新建一个ASP.NET Core Web Api项目,项目创建好后会有一个集成好的天气预报的类和控制器,接下来,我们的方法就在天气控制器里完成。
在启动项中添加Log配置替换默认的日志提供程序,在控制器中编写具体输出内容
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureLogging(logging => // 添加提供程序(通用主机)
{
logging.ClearProviders(); // 去掉默认添加的日志提供程序
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
logging.AddEventLog();
}).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup < Startup > ();
});
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController: ControllerBase
{
private readonly ILogger <WeatherForecastController> _logger;
public WeatherForecastController(ILogger <WeatherForecastController> logger)
{
_logger = logger;
}
/// <summary>
/// 获取日志输出 - 通用主机
/// </summary>
[HttpGet]
public void GetLogOutUniversalHost()
{
// 内置日志
_logger.LogTrace(1000, "log Trace msg");
_logger.LogDebug(1001, "log Debug msg");
_logger.LogInformation(1002, "log Information msg");
_logger.LogWarning(1003, "log Warning msg");
_logger.LogError(1004, "log Error msg");
_logger.LogCritical(1005, "log Critical msg");
}
}
直接在控制器中创建LoggerFactory时调用提供程序的Add方法,使用LoggerFactory来创建 ILogger
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController: ControllerBase
{
/// <summary>
/// 获取日志输出 - 非主机
/// </summary>
[HttpGet]
public void GetLogOutNonHost()
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning).AddFilter("System", LogLevel.Warning).AddFilter("LoggingConsoleApp.Program", LogLevel.Debug).AddConsole().AddEventLog();
});
ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("非主机模式输出log msg");
}
}
或者在项目的.csproj文件添加如下代码:
<ItemGroup>
<Content Update="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<appSettings>
<add key="WebSocketPort" value="45154" />
</appSettings>
<log4net>
<!--指定日记记录方式,以滚动文件的方式(文件记录)-->
<appender name="logInfoToFile" type="log4net.Appender.RollingFileAppender,log4net">
<!--日志路径-->
<file value="logs/log4net/info"/>
<!--是否是向文件中追加日志-->
<AppendToFile value="true"/>
<!--日志根据日期滚动-->
<RollingStyle value="Date"/>
<!--日志文件名格式为:info.2021-12-30.txt-->
<DatePattern value="'.'yyyy-MM-dd'.txt'"/>
<!--日志文件名是否是固定不变的-->
<StaticLogFileName value="false"/>
<!--布局-->
<layout type="log4net.Layout.PatternLayout,log4net">
<ConversionPattern value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="logErrorToFile" type="log4net.Appender.RollingFileAppender,log4net">
<file value="logs/log4net/error"/>
<AppendToFile value="true"/>
<RollingStyle value="Date"/>
<!--日志文件名格式为:error.2021-12-30.txt-->
<DatePattern value="'.'yyyy-MM-dd'.txt'"/>
<StaticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<ConversionPattern value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<!-- 根(父)logger -->
<root>
<!-- 支持级别:ALL -->
<level value="ALL" additivity="false"/>
<!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF-->
<!-- 支持的appender名称:logInfoToFile -->
<appender-ref ref="logInfoToFile"/>
</root>
<!-- logger名称为ApplicationInfoLog,阻止父logger中的appender -->
<logger name="ApplicationInfoLog" additivity="false">
<!-- 支持级别:ALL -->
<level value="ALL"/>
<!-- 支持的appender名称:logInfoToFile -->
<appender-ref ref="logInfoToFile"/>
</logger>
<logger name="ApplicationErrorLog" additivity="false">
<!-- 支持级别:ALL -->
<level value="ALL"/>
<!-- 支持的appender名称:logErrorToFile -->
<appender-ref ref="logErrorToFile"/>
</logger>
</log4net>
</configuration>
关于log4net.config配置的详细说明,可参考log4net配置文件详解
5.1 Log4Net 简单配置,输出至控制台
/// <summary>
/// 获取日志输出 - 第三方框架:Log4Net(简单配置,输出至控制台)
/// </summary>
[HttpGet]
public void GetLogOutWithLog4NetSimpleConfiguration()
{
ILoggerRepository repository = LogManager.CreateRepository(".NET Core RepositoryRepository");
// 默认简单配置,输出至控制台
BasicConfigurator.Configure(repository);
ILog log = LogManager.GetLogger(repository.Name, ".NET Core Log4net");
log.Info(".NET Core Log4net log");
log.Info("test log");
log.Error("error");
log.Info("linezero");
}
5.1 运行结果
5.2 Log4Net 增加配置,输出至文件
/// <summary>
/// 获取日志输出 - 第三方框架:Log4Net(增加配置,输出至文件)
/// </summary>
[HttpGet]
public void GetLogOutWithLog4NetComplexConfigurations()
{
ILoggerRepository repository = LogManager.CreateRepository(".NET Core RepositoryRepository");
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
ILog log = LogManager.GetLogger(repository.Name, ".NET Core Log4net");
log.Info(".NET Core Log4net log");
log.Info("test log");
log.Error("error");
log.Info("linezero");
}
5.2 运行结果
或者在项目的.csproj文件添加如下代码:
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<?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"
throwConfigExceptions="true"
internalLogLevel="info"
internalLogFile="${basedir}/logs/nlog/internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="allfile" fileName="${basedir}/logs/nlog/all/nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="${basedir}/logs/nlog/my/nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" />
<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
<target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- BlackHole -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
public class Program
{
public static void Main(string[] args)
{
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
}
catch(Exception exception)
{
// NLog: 捕获设置错误
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// 确保在应用程序退出前刷新和停止内部定时器/线程(避免Linux上的分段故障)
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup < Startup > ();
}).ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
}).UseNLog(); // NLog: 依赖性注入设置NLog
}
appsettings.json中指定的日志配置覆盖了对SetMinimumLevel的任何调用。因此,要么删除 "Default":要么根据你的需要正确调整。
上述logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
所以配置文件需要更改
{
"Logging": {
"LogLevel": {
"Default": "Trace", // 这里更改的地方
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
记得也要测试环境的配置,appsettings.Development.json,以免出现异常
{
"Logging": {
"LogLevel": {
"Default": "Trace", // 这个要更改
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
/// <summary>
/// 获取日志输出 - 第三方框架:NLog
/// </summary>
[HttpGet]
public void GetLogOutWithNLog()
{
// 内置日志
_logger.LogTrace(1000, "log Trace msg");
_logger.LogDebug(1001, "log Debug msg");
_logger.LogInformation(1002, "log Information msg");
_logger.LogWarning(1003, "log Warning msg");
_logger.LogError(1004, "log Error msg");
_logger.LogCritical(1005, "log Critical msg");
}
关于配置文件的说明可查看官网:NLog Configuration-file
还有一篇翻译文章方便查看:NLog类库使用探索——详解配置
public class Program
{
public static void Main(string[] args)
{
// 配置Serilog
Log.Logger = new LoggerConfiguration()
// 设定最小的记录级别
.MinimumLevel.Debug()
// 如果遇到Microsoft命名空间,那么最小记录级别为Information
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information)
// 记录相关上下文信息
.Enrich.FromLogContext()
// 日志输出到控制台
.WriteTo.Console()
// 输出到文件,指定输出路径和周期
.WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"logs/serilog/serilog.txt"), rollingInterval: RollingInterval.Day) // 配置日志输出文件,生成周期每天
.CreateLogger();
try
{
Log.Information("Starting up");
CreateHostBuilder(args).Build().Run();
}
catch(Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureLogging(logging =>
{
logging.ClearProviders(); // 去掉默认添加的日志提供程序
}).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup < Startup > ();
}).UseSerilog(); // 配置使用Serilog
}
这里为直接按照配置格式输出,当然也可以通过读取配置文件的形式读取配置的具体信息,本文不做具体阐述。
/// <summary>
/// 获取日志输出 - 第三方框架:Serilog
/// </summary>
[HttpGet]
public void GetLogOutWithSerilog()
{
// 内置日志
_logger.LogTrace(1000, "log Trace msg");
_logger.LogDebug(1001, "log Debug msg");
_logger.LogInformation(1002, "log Information msg");
_logger.LogWarning(1003, "log Warning msg");
_logger.LogError(1004, "log Error msg");
_logger.LogCritical(1005, "log Critical msg");
}
4)NLog vs log4net vs Serilog: Compare .NET Logging Frameworks
6)日志级别
以上就是.NET Core 日志记录程序和常用日志记录框架的简单使用的介绍,做此记录,如有帮助,欢迎点赞关注收藏!
手机扫一扫
移动阅读更方便
你可能感兴趣的文章