Logging to an individual log file for each individual thread

前端 未结 3 1324
灰色年华
灰色年华 2020-12-03 15:06

I have a service application that on startup reads an XML file and starts a thread for each entry in the XML file. Each thread creates an instance of a worker class which re

3条回答
  •  醉酒成梦
    2020-12-03 15:29

    @Adam's solution helped me. For posterity here's simple code that creates 5 Tasks (using threads from the threadpool), where in each task each thread writes to a separate log file (and then sleeps for 1 second), five times.

    .NET Framework 4.7.2
    .NET Console Application

    AssemblyInfo.cs

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    // General Information about an assembly is controlled through the following
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("LogfilePerThread")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("Microsoft")]
    [assembly: AssemblyProduct("LogfilePerThread")]
    [assembly: AssemblyCopyright("Copyright © Microsoft 2020")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    
    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components.  If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]
    
    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("d3ec8b29-e701-424e-80d4-473e9fbb5572")]
    
    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
    

    Program.cs

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using log4net;
    using log4net.Repository;
    
    namespace LogfilePerThread
    {
        class Program
        {
            static void Main(string[] args)
            {
                int nWorkers = 5; // 5 workers, 5 log files
                Task[] Workers = new Task[nWorkers];
                for (int i = 0; i < nWorkers; i++)
                {
                    int i1 = i;
                    Workers[i] = new Task(() => Work(i1));
                    Workers[i].Start();
                }
    
                Task.WaitAll(Workers);
                Console.WriteLine("Main executed.");
            }
    
            public static void Work(int workerID)
            {
                // Make sure the directory where logfiles are written exists
                string logfileName = $"Worker{workerID}_TaskLog"; // Logfile extension & path specified in App.config
                ILoggerRepository repository = LogManager.CreateRepository($"{logfileName}Repository");
                ThreadContext.Properties["WorkerLoggerProperty"] = logfileName;
                log4net.Config.XmlConfigurator.Configure(repository);
                ILog log = LogManager.GetLogger($"{logfileName}Repository", "WorkerLogger"); // Use this logger object in thread
    
                // Do work: Log & Sleep
                for (int i = 0; i < 5; i++)
                {
                    log.Info($"Logging from WorkerID={workerID} - Msg {i}");
                    Thread.Sleep(1000);
                }
            }
        }
    }
    

    App.config

    
    
      
        

    C:\logs\Worker0_TaskLog.log

    2020-01-14 21:59:46,696 [ INFO, 3] Logging from WorkerID=0 - Msg 0
    2020-01-14 21:59:47,714 [ INFO, 3] Logging from WorkerID=0 - Msg 1
    2020-01-14 21:59:48,714 [ INFO, 3] Logging from WorkerID=0 - Msg 2
    2020-01-14 21:59:49,714 [ INFO, 3] Logging from WorkerID=0 - Msg 3
    2020-01-14 21:59:50,714 [ INFO, 3] Logging from WorkerID=0 - Msg 4
    

    C:\logs\Worker1_TaskLog.log

    2020-01-14 21:59:46,696 [ INFO, 4] Logging from WorkerID=1 - Msg 0
    2020-01-14 21:59:47,714 [ INFO, 4] Logging from WorkerID=1 - Msg 1
    2020-01-14 21:59:48,714 [ INFO, 4] Logging from WorkerID=1 - Msg 2
    2020-01-14 21:59:49,714 [ INFO, 4] Logging from WorkerID=1 - Msg 3
    2020-01-14 21:59:50,714 [ INFO, 4] Logging from WorkerID=1 - Msg 4
    

    C:\logs\Worker2_TaskLog.log

    2020-01-14 21:59:46,697 [ INFO, 5] Logging from WorkerID=2 - Msg 0
    2020-01-14 21:59:47,713 [ INFO, 5] Logging from WorkerID=2 - Msg 1
    2020-01-14 21:59:48,713 [ INFO, 5] Logging from WorkerID=2 - Msg 2
    2020-01-14 21:59:49,713 [ INFO, 5] Logging from WorkerID=2 - Msg 3
    2020-01-14 21:59:50,713 [ INFO, 5] Logging from WorkerID=2 - Msg 4
    

    C:\logs\Worker3_TaskLog.log

    2020-01-14 21:59:46,696 [ INFO, 6] Logging from WorkerID=3 - Msg 0
    2020-01-14 21:59:47,714 [ INFO, 6] Logging from WorkerID=3 - Msg 1
    2020-01-14 21:59:48,714 [ INFO, 6] Logging from WorkerID=3 - Msg 2
    2020-01-14 21:59:49,714 [ INFO, 6] Logging from WorkerID=3 - Msg 3
    2020-01-14 21:59:50,714 [ INFO, 6] Logging from WorkerID=3 - Msg 4
    

    C:\logs\Worker4_TaskLog.log

    2020-01-14 21:59:47,580 [ INFO, 7] Logging from WorkerID=4 - Msg 0
    2020-01-14 21:59:48,580 [ INFO, 7] Logging from WorkerID=4 - Msg 1
    2020-01-14 21:59:49,580 [ INFO, 7] Logging from WorkerID=4 - Msg 2
    2020-01-14 21:59:50,580 [ INFO, 7] Logging from WorkerID=4 - Msg 3
    2020-01-14 21:59:51,580 [ INFO, 7] Logging from WorkerID=4 - Msg 4
    

提交回复
热议问题