2024-01-09 08:00:07

log4net vs. nlog

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

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

引用:

log4net

nlog

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


当前回答

对于我运行的一个开源项目,这是一个无耻的宣传,但是考虑到关于哪个. 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平台。

其他回答

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

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档案

我赞同上面的方法,并且更喜欢nLog。Entlib不必要地膨胀。

有一件事总是让我与Log4net忘记添加以下到全局。Asax初始化组件:

log4net.Config.XmlConfigurator.Configure();

首先查看堆栈的其余部分。

如果你正在使用NHibernate,它直接利用Log4Net。 其他框架可能有它们需要的其他特定的记录器。

除此之外:两者都很好。

我自己选择了Log4Net。配置起来很麻烦,如果配置不正确,找出哪里出了问题也很麻烦。但是,您可以让它做几乎任何您想从日志记录器。

如果你对Log4Net没有长期的问题,这是我写的一篇关于如何开始使用Log4Net的文章: http://elegantcode.com/2007/12/07/getting-started-with-log4net/

我最近被要求为一个即将到来的项目“创建一些登录的原型”。我没有任何日志框架的经验。我花了几天时间在Log4Net、NLog和Enterprise Library上研究、浏览教程、制作玩具应用程序等。3-4周后再回来,将它们整合成一个完整的演示版本。希望这些对你们有用。

我对我们项目的建议是:

使用日志facade(例如Common。日志,SimpleLoggingFacade)以避免直接依赖。 如果我们最终要将企业库用于其他功能,那么也要将其用于日志记录。 如果我们最终要使用依赖于Log4Net的东西,那么就使用Log4Net。 如果以上都不是,请使用NLog。我更喜欢这样。

这是基于以下发现(意见!):

All 3 frameworks are capable and can do some sophisticated things. We want a quality solution, but frankly don't need ultra high performance or 60 types of event sinks. All 3 have very similar basic concepts. Each has its own cool tricks, like really advanced routing, or dynamic log filenames, file truncating, etc. All 3 are pretty well documented in their own way. For a complete newb like me, they were all a little awkward initially. No drastic differences here for the basics. I got over it. When revisiting things a few weeks later, NLog was clearly the easiest to resume. I needed very little brush up on it. With Log4Net, I had to revisit a few online examples to get going. With EntLib, I gave up and did the tutorials all over again from scratch - I was totally lost. I couldn't figure out how to get EntLib to do some things like log to the database. It might be easy, but it was beyond my time limit. Log4Net and NLog have a small in-code footprint. EntLib is spammy, but I'd use a facade over it anyway. I accidentally mis-configured EntLib and it told me at run time. Log4Net didn't. I didn't have an accidental mis-config with NLog. EntLib comes with a nice looking app.config editor, which you 100% need. NLog has a config file schema so you get "intellisense". Log4Net comes with nada.

显然到目前为止我喜欢NLog。尽管有其他可用的解决方案,但还不足以使用它。

嗯. .我使用Enterprise库执行数据库日志记录任务 现在由于性能瓶颈,我切换到NLog。

一些比较信息:

http://pauliusraila.blogspot.com/2010/10/solving-database-logging-bottlenecks.html