从多个进程进行日志记录,nLog 丢失日志且存档不一致

use*_*969 5 c# logging nlog

我有一个进程在 Windows 服务器上运行,多个用户在多个会话上运行(可能有多达 50 个并发用户),我想:

  1. 将所有进程记录到单个日志文件
  2. 将日志大小限制为 1.5MB(出于我们测试的目的)
  3. 记录器具有可接受的性能

所以我想我应该尝试一下 nLog:

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
        internalLogFile="file.txt">
        <targets async="false">
          <target name="regFile" xsi:type="File"
                  layout="${longdate} [${windows-identity}][${processid}]${threadname} ${uppercase:${level}} ${callsite} - ${message}${onexception:${newline}${exception:format=tostring}}" 
                  fileName="${basedir}/logs/test.log" 
                  archiveFileName="${basedir}/logs/test.{#####}.log"
                  archiveAboveSize="102400"
                  archiveNumbering="Rolling"
                  maxArchiveFiles="14"
                  concurrentWrites="true"
                  keepFileOpen="true"               
                  autoFlush="true"                                      
                          />
        </targets>
        <rules>
                <logger name="*" minlevel="Trace" writeTo="regFile" />
        </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

我还写了一个小测试器:

class Program
    {
        private static Logger m_log = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            // Load logger configuration
            var assemblyLocation = Assembly.GetExecutingAssembly().Location;
            var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            if (assemblyDirectory != null)
            {
                var logConfig = new FileInfo(Path.Combine(assemblyDirectory, "nlogConfig.xml"));
                NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(logConfig.FullName, true);
            }
            if (args.Length == 1)
            {
                var sw = Stopwatch.StartNew();
                var size = Int32.Parse(args[0]);
                m_log.Info("Will launch {0} process and wait for them to come back...",size);                               
                var handles = new WaitHandle[size];  
                for (int i = 0; i < size; i++)
                {
                    var p = Process.Start(Assembly.GetExecutingAssembly().Location);
                    var processHandle = OpenProcess(
                            ProcessAccessFlags.Synchronize, false, p.Id);
                    if (processHandle != IntPtr.Zero)
                    handles[i] = new ManualResetEvent(false)
                    {
                        SafeWaitHandle = new SafeWaitHandle(processHandle, false)
                    };
                    m_log.Fatal("Started pid={0}.",p.Id);
                }

                m_log.Info("Created the processes, now wait.");
                WaitHandle.WaitAll(handles);
                sw.Stop();
                Thread.Sleep(100);
                m_log.Info("Done, took {0}.",sw.ElapsedMilliseconds);
            }
            else
            {
                m_log.Info("Running for {0} * {1}",1000,m_log.ToString());
                for (int i = 0; i < 1000; i++)
                {
                    m_log.Error("Hello nlog {0}", i);
                }              
            }           
        }
        #region Native API

        [Flags]
        enum ProcessAccessFlags
        {
            All = 0x001F0FFF,
            Terminate = 0x00000001,
            CreateThread = 0x00000002,
            VMOperation = 0x00000008,
            VMRead = 0x00000010,
            VMWrite = 0x00000020,
            DupHandle = 0x00000040,
            SetInformation = 0x00000200,
            QueryInformation = 0x00000400,
            Synchronize = 0x00100000
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId);
        #endregion
    }
Run Code Online (Sandbox Code Playgroud)

运行得很好,只要我不需要存档...运行我的测试器如下:“LoggerTester.exe 10”我得到

  1. 少于 10,000 条 ERROR 消息(每条 ERROR 中应写入 1000 条“hello nlog”)
  2. 似乎有时有 2 个记录器滚动文件,因此中间有一些小于 100kb 的日志。

也许我要求太多,我必须使用某种内存记录器模式(用 1 个记录器来实际管理文件),但我真的不希望这样做。

在任何地方都找不到任何对此的参考,所以我想我在这里试试运气......谢谢。

wag*_*ghe 0

我认为您将无法从多个进程登录到单个文件。我建议尝试其他一些目标:

  1. 数据库目标
  2. LogReceiverwebServiceTarget(NLog 也有一个 LogReceiverService 实现来配合目标)。
  3. 网络服务目标

我过去曾成功使用 DatabaseTarget。

我没有其他目标的经验,但我确实实现了一次 LoggingService 。它与 LogReceiverWebServiceTarget 非常相似。它是一个实现日志记录接口的 WCF 服务。我们有一个相应的目标,可以将其配置为与日志记录服务端点进行通信。我们还实现了一些缓存和刷新,以便我们发送消息块,而不是为每条消息进行服务调用。

祝你好运!