简体   繁体   English

C#写入日志文件

[英]C# writing to log file

I want to write some events and exceptions to a log file when my app is running. 我想在我的应用程序运行时将一些事件和异常写入日志文件。 The log file size is limited to N Megabytes. 日志文件的大小限制为N MB。 When the file size is more than N Megabytes, I want to overwrite from the start of the file. 当文件大小超过N MB时,我想从文件开头覆盖。 Since the file size is N megabytes I don't want to copy to temporary file and then rename it. 由于文件大小为N兆字节,因此我不想复制到临时文件然后重命名。

Give any suggestions/approach for the above problem. 提供有关上述问题的任何建议/方法。

尝试使用Microsoft的Log4Not或Enterprise Library日志记录组件

Either use Log4net (http://logging.apache.org/log4net/index.html) where you can do this 您可以在其中使用Log4net(http://logging.apache.org/log4net/index.html)

    logger.Debug("Here is a debug log.");
    logger.Info("... and an Info log.");
    logger.Warn("... and a warning.");
    logger.Error("... and an error.");
    logger.Fatal("... and a fatal error.");

or use the Enterprise Libaries (http://msdn.microsoft.com/en-us/library/ff648951.aspx) where you can do this 或使用企业库(http://msdn.microsoft.com/zh-cn/library/ff648951.aspx)在此处可以执行此操作

    LogEntry entry = new LogEntry()
                         {
                             Message = "Hello Ent. Lib. Logging"
                         };
    Logger.Write(entry);

Both can be configured to point to a file thru the web/app.config. 两者都可以配置为指向通过web / app.config的文件。

Check out log4net ! log4net It is really flexible and allows you to change the behaviour of logging at run-time (where to log / how to log / what to log ...). 它非常灵活,可以让您在运行时更改日志记录的行为(记录位置/记录方式/记录内容...)。

The RollingFileAppender allows you to define the behaviour when a given size-limit is reached, and it could probably answer your question. RollingFileAppender允许您定义达到给定大小限制时的行为,它可能会回答您的问题。

float N = 1; //Log File size in MB
FileInfo logFile = new FileInfo("c:\\myLogFile.txt");
if (logFile.Length > 1024 * N)
{
   logFile.Delete();
   logFile.Create();
}

using (StreamWriter logStream = logFile.AppendText())
   logStream.Write("String to write");

the simpliest way, and probably the worst. 最简单的方法,也可能是最坏的方法。 Edited to reflect Hans Kesting comments. 编辑以反映Hans Kesting的评论。

作为Log4Net的替代品,我可以推荐NLog 。在拥有自己的简单日志类之后,我们发现它更加透明且易于使用。

Why don't you try enterprise logging library from Microsoft which will give you many more features than the older FileIO operations or the log4net. 为什么不尝试使用Microsoft的企业日志记录库,它将为您提供比旧FileIO操作或log4net更多的功能。

Microsoft has released 5.0 version of enterprise library, which will help you write logs and also configuring them easily with a UI, give it a try. Microsoft已发布企业库的5.0版本,它将帮助您编写日志并通过UI轻松配置它们,请尝试一下。

If your question refers to the .Net trace listener which writes .svclog files then the answer is no: you cannot configure it to start writing from the beginning when it reaches a certain limit. 如果您的问题涉及写入.svclog文件的.Net跟踪侦听器,那么答案是否定的:您无法将其配置为在达到特定限制时从头开始写入。

However, Microsoft have a solution called Circular Tracing that you can try to circumvent the problem of log files growing without limit. 但是,Microsoft有一个称为“循环跟踪”解决方案 ,您可以尝试解决日志文件无限制增长的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM