您好,登录后才能下订单哦!
在C#中,有多种日志记录方案可供选择。以下是一些常见的日志记录库和方法:
NLog是一个功能强大的日志记录库,易于配置和使用。它支持多种日志输出方式,如文件、数据库、网络等。
dotnet add package NLog
创建一个nlog.config
文件:
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
<targets>
<file name="File" fileName="logs/app.log" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="File" />
</rules>
</nlog>
在代码中配置和使用NLog:
using NLog;
class Program
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
logger.Trace("This is a trace message.");
logger.Debug("This is a debug message.");
logger.Info("This is an info message.");
logger.Warn("This is a warning message.");
logger.Error("This is an error message.");
logger.Fatal("This is a fatal message.");
}
}
log4net是Apache的一个日志记录库,同样功能强大且易于配置。
dotnet add package log4net
创建一个log4net.config
文件:
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/app.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{context}] - %message%newline" />
</layout>
</appender>
<root>
<level value="Trace" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
在代码中配置和使用log4net:
using log4net;
class Program
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
logger.Trace("This is a trace message.");
logger.Debug("This is a debug message.");
logger.Info("This is an info message.");
logger.Warn("This is a warning message.");
logger.Error("This is an error message.");
logger.Fatal("This is a fatal message.");
}
}
Serilog是一个结构化的日志记录库,支持多种日志接收器和格式化选项。
dotnet add package Serilog
创建一个appsettings.json
文件:
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "logs/app.log",
"rollSizeLimit": 10485760, // 10MB
"rollCount": 5,
"formatter": "Serilog.Formatting.Compact"
}
}
]
}
}
在代码中配置和使用Serilog:
using Serilog;
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/app.log", rollingInterval: RollingInterval.Day, rollSizeLimit: 10MB)
.CreateLogger();
Log.Trace("This is a trace message.");
Log.Debug("This is a debug message.");
Log.Info("This is an info message.");
Log.Warn("This is a warning message.");
Log.Error("This is an error message.");
Log.Fatal("This is a fatal message.");
}
}
.NET Core和.NET 5+自带的日志记录库,易于集成和使用。
在Startup.cs
中配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddFile("logs/app.log", rollSizeLimit: 10485760, rollCount: 5);
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
在代码中配置和使用Microsoft.Extensions.Logging:
using Microsoft.Extensions.Logging;
class Program
{
private static readonly ILogger<Program> logger = LoggerFactory.CreateLogger<Program>();
static void Main(string[] args)
{
logger.LogTrace("This is a trace message.");
logger.LogDebug("This is a debug message.");
logger.LogInformation("This is an info message.");
logger.LogWarning("This is a warning message.");
logger.LogError("This is an error message.");
logger.LogCritical("This is a fatal message.");
}
}
选择哪种日志记录方案取决于你的具体需求和项目规模。NLog和log4net功能强大且成熟,适合大型项目;Serilog结构化和灵活,适合现代.NET应用;Microsoft.Extensions.Logging集成简单,适合.NET Core和.NET 5+项目。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。