从多进程使用单个文件用于Log4net?

时间:2015-11-12 09:56:57

标签: c# log4net

我遇到Log4net问题。我想在许多进程中使用Log4net,只使用单个文件日志。我有谷歌并试图添加 log4net.Appender.FileAppender + InterProcessLock log4net.Appender.FileAppender + MinimalLock 但它无法正常工作。 我用c#代码配置Log4net:

 RollingFileAppender appender = new RollingFileAppender();
            string logFilePath = Path.Combine(folder);
            appender.File = Path.Combine(logFilePath, fileName);
            appender.AppendToFile = true;
            appender.RollingStyle = RollingFileAppender.RollingMode.Size;
            appender.MaxSizeRollBackups = maxSizeRollBackups;
            appender.MaxFileSize = Convert.ToInt64(maxFileSize * 1024 * 1024);
            appender.StaticLogFileName = true;
            log4net.Layout.PatternLayout patternLayout = new log4net.Layout.PatternLayout();
            patternLayout.Header = GetHeader(continueFile);
            patternLayout.ConversionPattern = conversionPattern;
            patternLayout.ActivateOptions();
            appender.Layout = patternLayout;
            appender.ActivateOptions();
            appender.LockingModel = new FileAppender.InterProcessLock();

            //FileAppender aa = new FileAppender();

            try
            {
                Hierarchy hierarchy;

                if (isDesignTime)
                {
                    hierarchy = LogManager.GetRepository() as Hierarchy;
                    //hierarchy = LogManager.CreateRepository(loggerName) as Hierarchy;
                }
                else if (HasRepository(Tracer))
                {
                    hierarchy = LogManager.GetRepository(loggerName) as Hierarchy;
                }
                else
                {
                    hierarchy = LogManager.CreateRepository(loggerName) as Hierarchy;
                }

                hierarchy.Root.AddAppender(appender);
                hierarchy.Configured = true;


                //configure the logging at the root.  
                hierarchy.Root.Level = Level.All;
                }
}

我的应用会创建一个新流程来运行它。

如何为这种情况配置log4net?

0 个答案:

没有答案