【问题标题】:Logging in case of multiple tasks in windows service在 Windows 服务中有多个任务的情况下进行日志记录
【发布时间】:2020-04-17 11:31:14
【问题描述】:

我想将不同任务中的所有日志记录到不同的日志文件中,如下面的代码 sn-p 中所述。但是使用下面的代码,它确实将日志写入在不同任务中创建的所有日志文件,但是为任务编写的日志会合并到由不同任务编写的日志中。

       private static string logFile;
       private static void CreateEmptyFile(string filename)
       {
            if (!File.Exists(filename))
            {
                File.Create(filename).Dispose();
            }
       }

       private static void Log(string logMessage, string path)
       {
            string timeStamp = String.Format("{0} {1} - ", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
            File.AppendAllText(path, timeStamp + logMessage + Environment.NewLine);
       }

       private static void TaskMethod1(arg1)
       {
            // It calls a bunch of functions and they all log to logfile and all these operation including bunch of functions called(they also log using Log method)
            logFile = DateTime.Now.ToFileTime() + ".txt";
            CreateEmptyFile(logFile);
            Log("TaskMethod1", logFile);
       }
       private static async Task TaskMethod()
       {
           while(runningService)
           {
              // Thi will create more than one task in parallel to run and each task can take upto 30 minutes to finish. Based on some condition the upper limit for parallel tasks can go upto 10.
              Task.Run(() => TaskMethod1(arg1);
           }
       }
       internal static void Start()
        {
            runningService = true;
            Task1 = Task.Run(() => TaskMethod());
        }

我希望将不同任务的所有日志文件分开,并且一个任务的内容不与另一任务的内容合并。 我怀疑这是因为全局变量logFile 在创建新任务时会更改其值,但我不确定,这是将日志写入其他任务日志的原因吗? 如果这是问题,我该如何解决?我是否需要将logFile 作为参数传递给每个函数,而不是全局声明它。

【问题讨论】:

  • 你的 logFile 变量是静态的,所以所有的日志都会被写入 LAST 指定的路径。
  • 是的,在这种情况下您不能使用静态变量。另外请注意,使用 DateTime.Now 作为日志文件名可能会导致两个任务尝试使用相同的日志文件名。
  • 而且 File.AppendAllText() 也不是记录日志的好习惯(事实上,几乎任何事情)
  • 对于解决方案,在启动任务之前明确创建每个任务的日志文件名并将其传递给任务(例如作为方法参数),以便任务使用该日志文件。
  • @OguzOzgul 所以如果我删除 static 关键字并且我正在使用 DateTime.Now,它应该将日志写入每个单独的任务,对吧?如果我开始一项新任务,是否还会再次创建全局变量?我的意思是新任务会一起使用新的内存空间吗?

标签: c# multithreading windows-services task


【解决方案1】:

您可以在启动任务之前指定日志文件名,并将其作为参数传递给工作函数:

private static async Task TaskMethod()
{
    while (runningService)
    {
        // I am assuming this is not your original code which would create
        // a huge amount of parallel tasks without any checks..
        // if there is no sleep or no wait for synchronization, 
        // many tasks will be created in the same millisecond and will have
        // the same log file name, which would be the least of your problems!
        string logFile = DateTime.Now.ToFileTime() + ".txt";
        Task.Run(() => TaskMethod1(arg1, logFile));
    }
}

现在您的 TaskMethod1 应该如下所示:

// Does not compile. I only add the new string type parameter to the end.
// Your code is not complete, so the answer has to assume that you 
// understand that only one string logFile parameter is appended to the
// end of your parameter list
private static void TaskMethod1(arg1, string logFile)
{
    CreateEmptyFile(logFile);
    Log("TaskMethod1", logFile);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多