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

IAd*_*ter 90 .net c# performance logging winforms

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

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

有没有办法记录未使用外部框架记录的未处理的异常?

Bra*_*don 63

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();

}
Run Code Online (Sandbox Code Playgroud)

有关更多信息MSDN:

  • 你应该在文件中使用`using`,尽管因为它是一个方法的本地方法,所以无论如何都会很快被淹没. (21认同)
  • 还要记住,当`file.WriteLine(lines);`抛出异常时,代码永远不会命中`file.Close();`.使用`using`等同于`try {// using block} finally {// Dispose}`.这意味着即使`using`块中的代码抛出异常,该对象也将被处理掉, (18认同)
  • 如果在日志记录完成之前再次调用此方法会怎样?您将引发错误 - 进程无法访问文件"C:\ test.txt",因为它正由另一个进程使用.有谁知道这个问题吗? (5认同)

emp*_*mpi 22

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

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

Log4net已有详细记录,网上有数千个资源和用例.

  • 为什么这么多赞?该问题两次指出 OP 不想使用外部框架,并明确提到不想要 Log4net。当然,这应该是评论,而不是答案? (33认同)
  • 使用Nuget.使用外部库将变得轻而易举 (23认同)
  • @RyanfaeScotland你是对的,也许它不适合OP,但请不要忘记这是一个公共网站.由于问题的标题只是"如何登录c#",所以像我这样喜欢使用任何图书馆的人都会登陆这里并发现这个答案很有用.事实上,当我搜索"c#logging"时,这个帖子是第一个结果. (8认同)
  • 只需添加对项目的引用并放置一些xml配置 - 这非常简单.谷歌的log4net教程,并选择最适合你的. (4认同)
  • 原因是我从未在.net中使用任何外部库,所以我首先需要学习如何做;) (3认同)
  • 使用外部框架对于家庭项目来说很不错,但对于大型企业来说,开源项目是一个潜在的威胁,公司外的人员可以在项目中注入代码.所以它通常受到严格监管. (3认同)

šlj*_*ker 16

您可以直接写入事件日志.请查看以下链接:
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.");

    }
}
Run Code Online (Sandbox Code Playgroud)


liv*_*ove 15

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

System.IO.File.AppendAllText(@"c:\log.txt", "mymsg\n");
Run Code Online (Sandbox Code Playgroud)


jon*_*ezy 7

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


Ber*_* IT 6

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