2024-01-09 08:00:07

log4net vs. nlog

有人有这两方面的经验吗?它们之间有什么不同?

我们计划使用其中一个来登录企业应用程序。

引用:

log4net

nlog

编辑:我们对nlog或log4net没有现有的依赖关系。


当前回答

对于那些晚到这个线程的人,你可能想要回顾一下。net基类库(BCL)。在引入TraceSource类(大约在2005年)时,许多人忽略了。net 1.1和。net 2.0之间的变化。

使用TraceSource类似于其他日志框架,对日志进行粒度控制,在app.config/web中配置。配置和编程访问-没有企业应用程序块的开销。

.Net BCL团队博客:跟踪介绍-第一部分(也可以查看第二部分a,b,c)

还有很多比较:“log4net vs TraceSource”

其他回答

如果你去这里,你可以找到一个全面的矩阵,包括NLog和Log4Net库,以及企业库和其他产品。

有人可能会说,这个矩阵是为了强调矩阵中唯一的商业图书馆的特征。我认为这是正确的,但它是有用的,无论如何驱动我的选择与NLog。

问候

一个没有被过多讨论的关键考虑因素是支持和更新。

Log4Net自2006年4月19日发布1.2.10版本以来就没有更新过。

相比之下,NLog自2006年以来一直被积极支持,不久将发布NLog 2.0,支持许多在log4net上次更新时还不存在的平台,例如:

NET Framework 2.0 SP1及以上版本,3.5 & 4.0(客户端和扩展配置文件) Silverlight 2.0, 3.0, 4.0 .NET紧凑框架2.0,3.5 Mono 2。x档案

由于最近使用过这两个框架,我想我可以分享一下我对这两个框架的看法。

我被要求评估现有web应用程序的日志框架,在浏览了各种在线论坛后,我将选择范围缩小到NLog (v2.0)和log4net (v1.2.11)。以下是我的发现:

Setting/starting up with NLog is dead easy. You go through the Getting started tutorial on their website and you are done. You get a fair idea, how thing might be with nlog. Config file is so intuitive that anyone can understand the config. For example: if you want to set the internal logging on, you set the flag in Nlog config file's header node, which is where you would expect it to be. In log4net, you set different flags in web.config's appSettings section. In log4net, internal logging doesnt output timestamp which is annoying. In Nlog, you get a nice log with timestamps. I found it very useful in my evaluations. Filters in log4net - You better check my this question - log4net filter - how to write AND filter to ignore log messages and if you find an answer/solution for this, please let me know. I understand, there is a workaround for this question, as you can write your own custom filter. But something which is not easily available in log4net. Performance - I logged around 3000 log messages to database using a stored procedure. I used simple for loop (int i=0; i<3000; i++... to log the same message 3000 times. For the writes: log4net AdoAppender took almost double the time than NLog. Log4net doesnt support asynchronous appender.

经过比较,我选择了NLog作为日志框架。:)

正如我注意到的那样,log4net在应用程序运行的整个过程中都会锁定它们的输出文件,因此不能删除它们。除此之外,它们是相似的。

所以我更喜欢NLog。

对于我运行的一个开源项目,这是一个无耻的宣传,但是考虑到关于哪个. net日志框架更活跃的热烈讨论,我认为我应该发布一个到Serilog的必要链接。

要在应用程序中使用,Serilog类似于log4net(并大量借鉴了log4net)。然而,与其他. net日志选项不同的是,Serilog是关于保存日志事件的结构以供脱机分析。当你写:

Log.Information("The answer is {Answer}", 42);

大多数日志库立即将消息呈现为字符串。Serilog也可以这样做,但它保留了{Answer: 42}属性,以便稍后使用多个NoSQL数据存储之一时,可以根据Answer的值正确地查询事件。

我们已经接近1.0并支持所有的现代(。NET 4.5、Windows Store和Windows Phone平台。