无为清净楼资源网 Design By www.qnjia.com
Log4Net
相信大家都很熟悉了,算是比较主流和著名的日志组件了。
官网: logging.apache.org
开源地址: https://github.com/apache/logging-log4net
最佳实践
在项目中添加组件包
Install-Package log4net
添加 log4net.config
文件
<"1.0" encoding="utf-8" "log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net debug="false"> <appender name="info" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="log4net/info/" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="-1"/> <param name="MaximumFileSize" value="5MB"/> <param name="RollingStyle" value="Composite" /> <param name="DatePattern" value="yyyyMMdd\\HH".log"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n { "system": "Meowv.Blog", "datetime": "%d", "description": "%m", "level": "%p", "info": "%exception" }" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="INFO" /> </filter> </appender> <appender name="error" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="log4net/error/" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="-1"/> <param name="MaximumFileSize" value="5MB"/> <param name="RollingStyle" value="Composite" /> <param name="DatePattern" value="yyyyMMdd\\HH".log"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n { "system": "Meowv.Blog", "datetime": "%d", "description": "%m", "level": "%p", "info": "%exception" }" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> <root> <level value="ALL"></level> <appender-ref ref="info"/> <appender-ref ref="error"/> </root> </log4net> </configuration>
控制台项目
using log4net; using log4net.Config; using System; using System.IO; using System.Reflection; namespace ConsoleAppDemo { class Program { static void Main(string[] args) { var log4netRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config")); ILog log = LogManager.GetLogger(log4netRepository.Name, "NETCorelog4net"); log.Info("NETCorelog4net log"); log.Info("test log"); log.Error("error"); log.Info("linezero"); Console.ReadKey(); } } }
AspNetCore项目
新建一个静态类,写一个扩展方法 UseLog4Net()
,用于 log4net
初始化。
public static class Log4NetExtensions { public static IHostBuilder UseLog4Net(this IHostBuilder hostBuilder) { var log4netRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config")); return hostBuilder; } }
扩展方法返回 IHostBuilder
对象,这样可以链式调用,美化代码,然后在 Program.cs
中使用扩展方法 UseLog4Net()
。
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace WebAppDemo { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }).UseLog4Net(); } }
配置完成,然后在任意地方写入日志即可。
using log4net; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; namespace WebAppDemo.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILog _log; public WeatherForecastController() { _log = LogManager.GetLogger(typeof(WeatherForecastController)); } [HttpGet] public IEnumerable<WeatherForecast> Get() { _log.Info("Hello, this is a Weather api!"); _log.Warn("Wran !!!"); _log.Error("Controller Error"); var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } } }
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月06日
2024年11月06日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]