windows C# 日志记录。我应该使用什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4670933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:57:54  来源:igfitidea点击:

C# Logging. What should I use?

c#windowslogging

提问by Cole W

I'm looking at switching to a new unified logging solution for use in our new line of products and I wanted to see what some of the people on Stack Overflow thought. We will need logging for a variety of applicatons: ASP .net, windows services, web services, wpf apps etc. We are a windows shop only.

我正在考虑切换到新的统一日志记录解决方案以用于我们的新产品系列,我想看看 Stack Overflow 上的一些人是怎么想的。我们需要记录各种应用程序:ASP .net、windows 服务、web 服务、wpf 应用程序等。我们只是一家 windows 商店。

Some of our requirements for a logging solution include:

我们对日志记录解决方案的一些要求包括:

1) Log file management

1) 日志文件管理

- Ability to split files up over a certain size
- Ability to auto archive/delete after certain period of time

2) Ability to send emails on certain types of messages logged (errors for example)

2)能够就记录的某些类型的消息(例如错误)发送电子邮件

3) Ability to write messages to the windows event log

3) 能够将消息写入 Windows 事件日志

- We need to be able to specify where it's being written in the event log. 
  It would also be nice if it would automatically create the event log source if it does exist.

I've started looking at nLog, windows trace and log4net. I'm not limited to these 3 only it's just a few that came up a lot when searching.

我已经开始研究 nLog、windows trace 和 log4net。我不仅限于这三个,只是在搜索时出现了很多。

采纳答案by RQDQ

log4net is always a good choice.

log4net 始终是一个不错的选择。

http://logging.apache.org/log4net/

http://logging.apache.org/log4net/

回答by HuBeZa

Use .NET common logging. You can choose later a specific provider (NLog, CLog, log4net...), or even create custom ones.

使用.NET 通用日志记录。您可以稍后选择特定的提供程序(NLog、CLog、log4net...),甚至创建自定义的提供程序。

回答by Jorge Córdoba

And just another one: NLog.

还有一个:NLog

  • Files – single file or multiple, with automatic file naming and archival
  • Event Log – local or remote Database – store your logs in databases supported
  • by .NET Network – using TCP, UDP, SOAP, MSMQ protocols
  • Command-line console – including color coding of messages
  • E-mail – you can receive emails whenever application errors occur
  • ASP.NET trace
  • and many more
  • 文件 – 单个文件或多个文件,具有自动文件命名和存档功能
  • 事件日志 - 本地或远程数据库 - 将您的日志存储在支持的数据库中
  • 通过 .NET Network – 使用 TCP、UDP、SOAP、MSMQ 协议
  • 命令行控制台——包括消息的颜色编码
  • 电子邮件——您可以在发生应用程序错误时收到电子邮件
  • ASP.NET 跟踪
  • 还有很多

回答by pdiddy

You can take a look at Enterprise Library, they have an application block for logging which is extensible

您可以查看 Enterprise Library,它们有一个可扩展的用于日志记录的应用程序块

http://entlib.codeplex.com/

http://entlib.codeplex.com/

http://entlib.codeplex.com/releases/view/46741

http://entlib.codeplex.com/releases/view/46741

you can download the dev guide pdf, they have a section on logging (Chapter 4)

你可以下载开发指南 pdf,他们有一个关于日志的部分(第 4 章)

回答by Sly Gryphon

See this question for another comprehensive answer: When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

请参阅此问题以获得另一个全面的答案:我何时应该使用 Tracing vs Logger.NET、Enterprise Library、log4net 或 Ukadc.Diagnostics?

In short, the main logging frameworks available are the built-in .NET Framework System.Diagnostics, log4net, NLog and Enterprise Library Logging Application Block.

简而言之,可用的主要日志框架是内置的 .NET Framework System.Diagnostics、log4net、NLog 和 Enterprise Library Logging Application Block。

A comparison of these main frameworks is available at: https://essentialdiagnostics.codeplex.com/wikipage?title=Comparison

这些主要框架的比较可在以下网址获得:https: //essentialdiagnostics.codeplex.com/wikipage?title=Comparison

1) Log file management

1) 日志文件管理

All the main frameworks listed above support rolling files but I think they leave the cleanup to you.

上面列出的所有主要框架都支持滚动文件,但我认为它们将清理工作留给了您。

e.g. in the past I've used a scheduled Windows job that uses robocopy with with "/mov /minage X" to move old files elsewhere, then delete or whatever.

例如,过去我使用了一个预定的 Windows 作业,该作业使用带有“/mov /minage X”的 robocopy 将旧文件移动到其他地方,然后删除或其他任何内容。

The EventSchemaTraceListener used in System.Diagnostics, which I blogged about recently, has a LimitedCircularFiles option, but not much tool support for viewing the logs (they are in XML).

System.Diagnostics 中使用的 EventSchemaTraceListener(我最近在博客中讨论过)有一个 LimitedCircularFiles 选项,但没有太多工具支持查看日志(它们是 XML 格式)。

2) Ability to send emails on certain types of messages logged (errors for example)

2)能够就记录的某些类型的消息(例如错误)发送电子邮件

All the main frameworks listed above support this either directly or through extensions (additional listeners).

上面列出的所有主要框架都直接或通过扩展(附加侦听器)支持这一点。

3) Ability to write messages to the windows event log

3) 能够将消息写入 Windows 事件日志

Again, all of the main frameworks support this, however I generally would recommend that writing to the Windows event log should be done directly and not via tracing.

同样,所有主要框架都支持这一点,但是我通常建议直接写入 Windows 事件日志而不是通过跟踪。

One of the issues is what you asked about for automatic creation of sources.

问题之一是您询问自动创建源的问题。

Any writing to the event log (that goes through EventLog.WriteEvent) willautomatically attempt to create the source on the first log message if it doesn't exist -- the problem is with security, only administrators are allowed to create sources, so running as a normal user this fails.

任何写入事件日志(通过 EventLog.WriteEvent)自动尝试在第一条日志消息上创建源,如果它不存在——问题在于安全,只有管理员被允许创建源,所以运行作为普通用户,这失败了。

Because of this you really do need to add an EventLogInstaller, so that the source is created at install time (installation is done by administrator). Once created, any process can then write to the source.

因此,您确实需要添加一个 EventLogInstaller,以便在安装时创建源(安装由管理员完成)。创建后,任何进程都可以写入源。

This then leads to my recommendation that you need to create and write to the event log in code in order to ensure the event log source is the same. Also, if writing to the event log generally you don't want to be able to 'turn it off' through misconfiguration.

这导致我建议您需要在代码中创建和写入事件日志,以确保事件日志源相同。此外,如果通常写入事件日志,您不希望能够通过错误配置“关闭它”。

My personal recommendation is for the out of the box .NET Framework System.Diagnostics, in particular the Service Trace Viewer is excellent for diagnosing issues, particular in multi-tier, multi-threaded environments if using WCF where it can pass correlation identifies across tiers.

我个人的建议是开箱即用的 .NET Framework System.Diagnostics,特别是 Service Trace Viewer 非常适合诊断问题,尤其是在多层、多线程环境中,如果使用 WCF,它可以跨层传递相关性标识.

回答by Remi Smirra

http://alexworx.github.io/ALox-Logging-Library/

http://alexworx.github.io/ALox-Logging-Library/

is a pretty straight forward library which is great if you have high demands on performance.

是一个非常直接的库,如果您对性能有很高的要求,它会很棒。

回答by Salvador Sarpi

You can create your own logging lib... I did.

您可以创建自己的日志库...我做到了。

Take a look at PostSharp http://www.sharpcrafters.com/they have very good examples of some basic logging systems.

看看 PostSharp http://www.sharpcrafters.com/他们有一些基本日志系统的很好的例子。