.net - 如何在不使用第三方库的情况下登录C#?


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


IAdapter asked 2019-09-11T08:25:57Z
62 votes
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);




Brandon answered 2019-09-11T08:26:12Z
23 votes

我宁愿不使用任何外部   像log4j.net这样的框架。

为什么? Log4net可能会满足您的大多数要求。例如,检查此类:RollingFileAppender。


empi answered 2019-09-11T08:26:50Z
16 votes

您可以直接写入事件日志。 检查以下链接:


using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
             //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("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.

        // 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.");

šljaker answered 2019-09-11T08:27:33Z
13 votes

如果您正在寻找一种真正简单的记录方式,您可以使用这一个衬垫。 如果该文件不存在,则创建该文件。

System.IO.File.AppendAllText(@"c:\log.txt", "mymsg\n");
live-love answered 2019-09-11T08:27:58Z
7 votes

我曾经写过自己的错误记录,直到我发现了ELMAH。 我从来没有能够像ELMAH那样完美地收到电子邮件部分。

jonezy answered 2019-09-11T08:28:22Z
6 votes

如果您想要接近.NET,请查看Enterprise Library Logging Application Block。 看这里。 或者快速入门教程检查一下。 我使用了企业库中的验证应用程序块,它非常适合我的需求,并且非常容易在项目中“继承”(安装它并引用它!)。

Bernoulli IT answered 2019-09-11T08:28:46Z
4 votes

如果您想要自己的自定义错误记录,您可以轻松编写自己的代码。 我会从我的一个项目中给你一个片段。

public void SaveLogFile(object method, Exception exception)
    string location = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\FolderName\";
        //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");


SaveLogFile(MethodBase.GetCurrentMethod(), `q`);
MisdartedPenguin answered 2019-09-11T08:29:16Z
translate from https://stackoverflow.com:/questions/5057567/how-do-i-do-logging-in-c-sharp-without-using-3rd-party-libraries