在C#.NET中创建日志文件并动态更新状态

在C#.NET中创建日志文件并动态更新状态,c#,.net,c#-4.0,C#,.net,C# 4.0,我想在我的控制台应用程序中创建格式为(log+datetime.Now).txt的日志文件 对于要记录的第一个状态,我要创建此日志文件。 我需要在10到20分钟的时间跨度内,将所有最新状态消息(大约50到60条消息)添加到此文件 同时,在这个时间范围内,如果用户打开这个文件,他应该能够自由地打开它 任何代码样本将不胜感激 感谢使用创建文件流。 记事本可以在其他人写入时打开文本文件(尽管它仅在打开时是最新的)。尝试使用StreamWriter。比如: using (StreamWriter wri

我想在我的控制台应用程序中创建格式为(log+datetime.Now).txt的日志文件

对于要记录的第一个状态,我要创建此日志文件。 我需要在10到20分钟的时间跨度内,将所有最新状态消息(大约50到60条消息)添加到此文件

同时,在这个时间范围内,如果用户打开这个文件,他应该能够自由地打开它

任何代码样本将不胜感激


感谢使用创建文件流。

记事本可以在其他人写入时打开文本文件(尽管它仅在打开时是最新的)。

尝试使用StreamWriter。比如:

using (StreamWriter writer = new StreamWriter("log.txt"))
{
            writer.WriteLine("Text here");
}

使用现有的日志框架,而不是滚动您自己的日志类。例如,可以使用最小锁定方法,使其他进程能够读取文件:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message %date%newline" />
    </layout>
</appender>

)。

您可以使用
StreamWriter
类来编写文件,甚至在文件中添加和添加文本

几天前,我在
日志记录
上写了一篇文章

我的博客帖子中的代码片段

        // Open the file using stream write.
        // If file does not exist, StreamWriter will create it.
        // Use the overloaded constructor of StreamWriter and 
        // pass second parameter as true for appending text to file.
        using (StreamWriter writer = new StreamWriter(@"D://myfile.txt", true))
        {
            // write the text to writer
            writer.WriteLine("Your text here" + " - " + DateTime.Now);

            // clear all the buffer and 
            // write the buffered data to text file.
            writer.Flush();
        }
注意:我对blogpost中的代码做了一些修改,以满足OP的要求。

旋转文件 跟踪当前文件的打开时间,如果超过给定的
时间间隔(“10-20分钟”),则关闭该文件,创建文件名并打开该文件

允许其他人读取该文件 这都是关于控制文件共享选项的,而其他方法将使用正确的默认值,如果我需要一些特定的东西,我宁愿显式地使用。具有正确的语义:

允许随后打开文件进行读取

解决方案
但是在实现了
MakeFileName
和设置
timeout

的构造函数之后,您希望他们能够在应用程序仍在日志记录时打开并编辑文件吗?或者,当应用程序登录到该文件时,用户只需要对其进行只读访问吗?用户在登录时只需要进行只读访问。我注意到,即使用户打开,该文件也将更新,即使用户打开。看看一个成熟的日志系统,这样做很容易。企业库有您想要的,并且非常易于使用。的副本
public static Boolean CreateLogFile(String message)
        {
            try
      {
                //string location = @"C://IRPC//myfile1.txt";
                string location = System.Environment.CurrentDirectory + "\\log " + LogTime + ".txt";
                if (!File.Exists(location))
                {
                    FileStream fs;
                    using (fs = new FileStream(location, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    {
                    }
                    fs.Close();
                }

                Console.WriteLine(message);
                //Release the File that is created
                StreamWriter sw = new StreamWriter(location, true);
                sw.Write(message + Environment.NewLine);
                sw.Close();
                sw = null;
                return true;
      }
      catch(Exception ex)
            {
                EventLog.WriteEntry("MIDocShare", "Error in CreateLogFile" + ex.Message.ToString(), EventLogEntryType.Error, 6000);
       return false;
      }
     }
public static Boolean CreateLogFile(String message)
        {
            try
      {
                //string location = @"C://IRPC//myfile1.txt";
                string location = System.Environment.CurrentDirectory + "\\log " + LogTime + ".txt";
                if (!File.Exists(location))
                {
                    FileStream fs;
                    using (fs = new FileStream(location, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    {
                    }
                    fs.Close();
                }

                Console.WriteLine(message);
                //Release the File that is created
                StreamWriter sw = new StreamWriter(location, true);
                sw.Write(message + Environment.NewLine);
                sw.Close();
                sw = null;
                return true;
      }
      catch(Exception ex)
            {
                EventLog.WriteEntry("MIDocShare", "Error in CreateLogFile" + ex.Message.ToString(), EventLogEntryType.Error, 6000);
       return false;
      }
     }