登录 C# 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/147557/
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
Error logging in C#
提问by Rodney Schuler
I am making my switch from coding in C++ to C#. I need to replace my C++ error logging/reporting macro system with something similar in C#.
我正在从 C++ 编码转换为 C#。我需要用 C# 中类似的东西替换我的 C++ 错误记录/报告宏系统。
In my C++ source I can write
在我的 C++ 源代码中,我可以写
LOGERR("Some error"); or LOGERR("Error with inputs %s and %d", stringvar, intvar);
LOGERR("一些错误"); 或 LOGERR("输入 %s 和 %d 出错", stringvar, intvar);
The macro & supporting library code then passes the (possibly varargs) formatted message into a database along with the source file, source line, user name, and time. The same data is also stuffed into a data structure for later reporting to the user.
宏和支持库代码然后将(可能是可变参数)格式的消息与源文件、源行、用户名和时间一起传递到数据库中。同样的数据也被塞进一个数据结构中,以便以后报告给用户。
Does anybody have C# code snippets or pointers to examples that do this basic error reporting/logging?
有没有人有 C# 代码片段或指向执行此基本错误报告/记录的示例的指针?
Edit:At the time I asked this question I was really new to .NET and was unaware of System.Diagnostics.Trace. System.Diagnostics.Trace was what I needed at that time. Since then I have used log4net on projects where the logging requirements were larger and more complex. Just edit that 500 line XML configuration file and log4net will do everything you will ever need :)
编辑:当我问这个问题时,我对 .NET 真的很陌生,不知道 System.Diagnostics.Trace。System.Diagnostics.Trace 正是我当时所需要的。从那以后,我在日志记录要求更大、更复杂的项目中使用了 log4net。只需编辑那个 500 行的 XML 配置文件,log4net 就会完成您需要的一切:)
采纳答案by Chris S
Lots of log4net advocates here so I'm sure this will be ignored, but I'll add my own preference:
这里有很多 log4net 拥护者,所以我相信这会被忽略,但我会添加我自己的偏好:
System.Diagnostics.Trace
This includes listeners that listen for your Trace()
methods, and then write to a log file/output window/event log, ones in the framework that are included are DefaultTraceListener
, TextWriterTraceListener
and the EventLogTraceListener
. It allows you to specify levels (Warning,Error,Info) and categories.
这包括听你的听众Trace()
方法,然后写入日志文件/输出窗口/事件日志,在框架中那些包含有DefaultTraceListener
,TextWriterTraceListener
和EventLogTraceListener
。它允许您指定级别(警告、错误、信息)和类别。
Trace class on MSDN
Writing to the Event Log in a Web Application
UdpTraceListener - write log4net compatible XML messages to a log viewer such as log2console
MSDN 上的跟踪类
写入 Web 应用程序中的事件日志
UdpTraceListener - 将兼容 log4net 的 XML 消息写入日志查看器,例如 log2console
回答by Ben Hoffstein
Even though I personally hate it, log4netseems to be the de facto standard for C# logging. Sample usage:
尽管我个人讨厌它,但log4net似乎是 C# 日志记录的事实上的标准。示例用法:
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Error(“Some error”);
log.ErrorFormat("Error with inputs {0} and {1}", stringvar, intvar);
回答by Mitch Wheat
回答by FryHard
Log4Netis a rather comprehensive logging framework that will allow you to log to different levels (Debug, Error, Fatal) and output these log statements to may different places (rolling file, web service, windows errors)
Log4Net是一个相当全面的日志框架,它允许你记录到不同的级别(调试、错误、致命)并将这些日志语句输出到不同的地方(滚动文件、Web 服务、Windows 错误)
I am able to easily log anywhere by creating an instance of the logger
我可以通过创建记录器的实例轻松地在任何地方登录
private static readonly ILog _log = LogManager.GetLogger(typeof([Class Name]));
and then logging the error.
然后记录错误。
_log.Error("Error messsage", ex);
回答by Eric Schoonover
Enterprise Libraryis a solid alternative to log4netand it offers a bunch of other capabilities as well (caching, exception handling, validation, etc...). I use it on just about every project I build.
Enterprise Library是log4net的可靠替代品,它还提供了许多其他功能(缓存、异常处理、验证等)。我几乎在我构建的每个项目中都使用它。
Highly recommended.
强烈推荐。
回答by Ted
Ditto for log4net. I'm adding my two bits because for actual use, it makes sense to look at some open source implementations to see real world code samples with some handy additions. For log4net, I'd suggest off the top of my head looking at subtext. Particularly take a look at the application start and assemblyinfo bits.
log4net 也是如此。我添加我的两个部分是因为对于实际使用,查看一些开源实现以查看带有一些方便添加的真实世界代码示例是有意义的。对于 log4net,我建议先看看subtext。特别看一下 application start 和 assemblyinfo 位。
回答by Mats Fredriksson
回答by Chris Ballard
Further to the couple of comments realting to the use of the System.Diagnostics methods for logging, I would also like to point out that the DebugViewtool is very neat for checking debug output when needed - unless you require it, there is no need for the apps to produce a log file, you just launch DebugView as and when needed.
除了关于使用 System.Diagnostics 方法进行日志记录的几条评论之外,我还想指出DebugView工具非常适合在需要时检查调试输出 - 除非您需要它,否则不需要生成日志文件的应用程序,您只需在需要时启动 DebugView。
回答by Mario
Log4Net, as others have said, is fairly common and similar to Log4j which will help you if you ever do any Java.
正如其他人所说,Log4Net 相当普遍,并且与 Log4j 类似,如果您使用过任何 Java,它将对您有所帮助。
You also have the option of using the Logging Application Block http://www.codeproject.com/KB/architecture/GetStartedLoggingBlock.aspx
您还可以选择使用 Logging Application Block http://www.codeproject.com/KB/architecture/GetStartedLoggingBlock.aspx
回答by CSharpAtl
You can use built in .NET logging. Look into TraceSource and TraceListeners, they can be configured in the .config file.
您可以使用内置的 .NET 日志记录。查看 TraceSource 和 TraceListeners,它们可以在 .config 文件中配置。