在C#错误日志记录 [英] Error logging in C#

查看:844
本文介绍了在C#错误日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++编码到C#使我的开关。我需要替换我的C ++错误日志/报告宏观系统,在C#中类似的东西。

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 ++源代码我可以写

In my C++ source I can write

LOGERR(某些错误);
要么
LOGERR(错误与输入%s和%D,STRINGVAR,intvar);

LOGERR("Some error"); or LOGERR("Error with inputs %s and %d", stringvar, intvar);

宏&放大器;支持库code则与源文件,源极线,用户名和时间一起传递(可能可变参数)格式的消息到数据库中。同样的数据也塞进数据结构后向用户报告。

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.

是否有人有C#code片段或指针到这样做基本的错误报告/记录的例子吗?

Does anybody have C# code snippets or pointers to examples that do this basic error reporting/logging?

编辑:是在我问这个问题,我是真正的新.NET和不知道System.Diagnostics.Trace的时间。 System.Diagnostics.Trace是我需要的东西在那个时候。从那时起,我已经用在哪里记录要求是更大,更复杂的项目log4net的。只需编辑500行的XML配置文件和log4net的会做你永远都需要的一切:)

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 :)

推荐答案

log4net的大量的主张在这里,所以我敢肯定,这将被忽略,但我会添加自己的preference:

Lots of log4net advocates here so I'm sure this will be ignored, but I'll add my own preference:

System.Diagnostics.Trace

这包括听你的跟踪()方法,然后写入日志文件/输出窗口/事件日志中所包含的听众,那些在框架中是 DefaultTraceListener TextWriterTraceListener会 EventLogTraceListener 。它允许你指定水平(警告,错误信息)和类别。

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类
<一href=\"http://stackoverflow.com/questions/286060/what-do-i-need-to-change-to-alllow-my-iis7-asp-net-3-5-application-to-create-an/7848414#7848414\">Writing事件日志在Web应用程序
结果
<一href=\"http://www.anotherchris.net/log4net/udptracelistener-a-udp-tracelistener-compatible-with-log4netlog4j/\">UdpTraceListener - 写log4net的兼容XML消息到一个日志查看器,如log2console

这篇关于在C#错误日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆