-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Log4Net 日志集成到 ILogger
ansonzhang edited this page Dec 11, 2019
·
1 revision
Blog.Core
├─ Controllers
│ └─ WeatherForecastController.cs
├─ Log4net.config // 就是这里
├─ Program.cs
├─ Properties
│ └─ launchSettings.json
├─ Startup.cs
├─ appsettings.Development.json
└─ appsettings.json
config 的内容是:
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- 将日志以回滚文件的形式写到文件中 -->
<!-- 按日期切分日志文件,并将日期作为日志文件的名字 -->
<appender name="RollingFileAppenderNameByDate" type="log4net.Appender.RollingFileAppender">
<!-- 日志文件存放位置,可以为绝对路径也可以为相对路径 -->
<file value="Log/" />
<!-- 将日志信息追加到已有的日志文件中-->
<appendToFile value="true" />
<!-- 最小锁定模式,以允许多个进程可以写入同一个文件 -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!-- 指定按日期切分日志文件 -->
<rollingStyle value="Date" />
<!-- 日志文件的命名规则 -->
<datePattern value=""GlobalExcepLogs_"yyyyMMdd".log"" />
<!-- 当将日期作为日志文件的名字时,必须将staticLogFileName的值设置为false -->
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date| %newline%message%newline--------------------------------%newline" />
<!-- 这里也可以直接用json格式输出,具体请自行百度 -->
</layout>
</appender>
<root>
<!-- 控制级别,由低到高:ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
<!-- 比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录 -->
<!-- 如果没有定义LEVEL的值,则缺省为DEBUG -->
<level value="ALL" />
<!-- 按日期切分日志文件,并将日期作为日志文件的名字 -->
<appender-ref ref="RollingFileAppenderNameByDate" />
</root>
</log4net>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.0.3" />
</ItemGroup>
XmlDocument log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead("Log4net.config"));//注意名字
var repo = log4net.LogManager.CreateRepository(
Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// 就是这里
loggerFactory.AddProvider(new Log4NetProvider("Log4net.config"));
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogError("this is an error.");
var rng = new Random();
return null;
}
有疑问,请自行查看博客园文章:https://www.cnblogs.com/laozhang-is-phi/p/9495618.html#autoid-1-0-0
或者加 QQ 群:867095512
-
- AOP
- Appsettings
- Async-Await
- Authorization-Ids4
- Authorization-JWT
- AutoMapper
- CORS
- DI-AutoFac
- DI-NetCore
- Filter
- GlobalExceptionsFilter
- HttpContext
- Log4
- MemoryCache
- Middleware
- MiniProfiler
- publish
- Redis
- Repository
- SeedData
- SignalR
- SqlSugar
- SqlSugar-Codefirst&DataSeed
- SqlSugar-SqlAOP
- Swagger
- T4
- Test-xUnit
- Temple-Nuget
- FAQ page is a good place to see whether your question is already asked.
- Ask a question in cnblogs if you need help.
- Submit an issue if you found a bug or have a feature request.
- Open a pull request when you prepared to contribute. Before that, it is encouraged to open an issue to discuss.