asp.net core 集成 log4net 日志框架

asp.net core 集成 log4net 日志框架

Intro

在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 LoggerProvider 的话就需要借助第三方日志框架实现了,而一些第三方框架的实现大多比较完善和成熟,不失为一个好办法。

自己写了一个 log4net 的扩展 WeihanLi.Common.Logging.Log4Net,提供了在 .net core 中使用 log4net 的扩展

安装 nuget 包

通过 nuget 安装 WeihanLi.Common.Logging.Log4Net

使用

基本使用

ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddLog4Net(); // loggerFactory.AddLog4Net(log4netConfigFilePath);

你可以在 asp.net core 应用里你的 Startup 文件中使用下面代码进行配置

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddLog4Net(); // loggerFactory.AddLog4Net(log4netConfigFilePath);

            // ...
        }

默认使用当前目录下的 log4net.config 文件作为 log4net 的配置文件,如果不是需要自己设置 log4net 配置文件的路径。

log4net 配置参考 示例配置

Memo

如果有什么问题或建议,欢迎指出

猜你喜欢

转载自www.cnblogs.com/weihanli/p/10454728.html
今日推荐