我如何在C#中进行日志logging?

我想在我的应用程序中实现日志logging,但宁愿不使用任何外部框架,如log4net。

所以我想做一些像DOS的回应文件。 什么是最有效的方法呢?

有没有办法login未经处理的exceptionlogging而不使用外部框架?

public void Logger(String lines) { // Write the string to a file.append mode is enabled so that the log // lines get appended to test.txt than wiping content and writing the log System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt",true); file.WriteLine(lines); file.Close(); } 

更多信息MSDN :

您可以使用内置的System.Diagnostics.TraceSource 。
以下是内置跟踪侦听器 + FileLogTraceListener的列表 。

网上有很多这样的手册, 或者Jeff Atwood +的改进手册 。

我宁愿不使用任何外部框架,如log4j.net。

为什么? Log4net可能会解决您的大部分需求。 例如选中这个类: RollingFileAppender 。

Log4net有很好的文档logging,网上有上千种资源和用例。

您可以直接写入事件日志。 检查以下链接:
http://support.microsoft.com/kb/307024
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

这里是MSDN的示例:

 using System; using System.Diagnostics; using System.Threading; class MySample{ public static void Main(){ // Create the source, if it does not already exist. if(!EventLog.SourceExists("MySource")) { //An event log source should not be created and immediately used. //There is a latency time to enable the source, it should be created //prior to executing the application that uses the source. //Execute this sample a second time to use the new source. EventLog.CreateEventSource("MySource", "MyNewLog"); Console.WriteLine("CreatedEventSource"); Console.WriteLine("Exiting, execute the application a second time to use the source."); // The source is created. Exit the application to allow it to be registered. return; } // Create an EventLog instance and assign its source. EventLog myLog = new EventLog(); myLog.Source = "MySource"; // Write an informational entry to the event log. myLog.WriteEntry("Writing to event log."); } } 

如果你正在寻找一个真正简单的方法来login,你可以使用这一个class轮。 如果该文件不存在,则创build该文件。

 System.IO.File.AppendAllText(@"c:\log.txt", "mymsg\n"); 

我曾经写我自己的错误logging,直到我发现ELMAH 。 我从来没有能够像ELMAH那样完美地把电子邮件的部分弄下来。

如果您希望保持接近.NET,请查看企业库日志logging应用程序块 。 看这里 或者快速入门教程检查这一点 。 我已经使用了企业库中的validation应用程序块,它非常适合我的需求,并且很容易在项目中“inheritance”(安装并引用!)。

我正在使用NLOG 。 这是辉煌的。

如果你想自己定制的错误logging,你可以很容易地编写自己的代码。 我会给你一个我的项目的片段。

 public void SaveLogFile(object method, Exception exception) { string location = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\FolderName\"; try { //Opens a new file stream which allows asynchronous reading and writing using (StreamWriter sw = new StreamWriter(new FileStream(location + @"log.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) { //Writes the method name with the exception and writes the exception underneath sw.WriteLine(String.Format("{0} ({1}) - Method: {2}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), method.ToString())); sw.WriteLine(exception.ToString()); sw.WriteLine(""); } } catch (IOException) { if (!File.Exists(location + @"log.txt")) { File.Create(location + @"log.txt"); } } } 

然后,实际写入错误日志只是写( q是捕获的exception)

 SaveLogFile(MethodBase.GetCurrentMethod(), `q`); 

我会build议“Postsharp ”。 它提供了非常优雅的框架来实现日志logging而不修改任何业务逻辑

我认为我们不应该重新发明轮子。 由于伐木很关心,我们在市场上有很好的select。 我还创build了一个小型的log4net包装,它非常易于使用,您只需要安装它就可以完成所有需要的configuration。

你可以在https://www.nuget.org/profiles/techy.rahulgargfind它的nuget。; 我还写了一个小博客来解释这个模块,你可以在http://www.codingeek.com/dotnet/logger上find这个模块;