【问题标题】:Use multiple versions of nlog使用多个版本的 nlog
【发布时间】:2012-09-18 08:27:54
【问题描述】:

我是nlog的新手,如果我错过了一些琐碎的事情,请原谅我的无知。

我使用 IIS7 托管多个应用程序,一些使用 nlog 1.0.0.505,一些使用 NLog 2.0。我不确定如何创建一个 web.config 来适应这两个版本。

是否可以将多个版本的 nlog.dll 放入 GAC,以便每个应用程序都有一个可供选择的版本列表?

从 GAC 中删除 nlog 并让应用程序使用部署时使用的 nlog 是否更好?在这种情况下,我如何在 web.config 中引用 nlog(设置目标等)

非常感谢任何帮助或指向我可以阅读更多内容的地方。

提前致谢。

【问题讨论】:

    标签: asp.net iis-7 web-config gac nlog


    【解决方案1】:

    您需要为每个应用程序或至少为每个使用不同版本的依赖 dll 的应用程序创建单独的应用程序域。单个 App Domain 无法加载同一个 dll 的多个版本。

    【讨论】:

      【解决方案2】:

      我们最终让应用程序提供了 nlog.dll 文件,并且没有在全局 web.config 中指定版本号。

      【讨论】:

        【解决方案3】:

        经过一番战斗......(如果你不想听故事,只需复制下面的代码)

        首先让我们说 GAC(全局汇编缓存)被发明的原因之一是在同一个地方使用多个版本的 dll。

        所以..我有一个带有 NLog 2.0 的 Sharepoint 项目,我需要在其中添加一个依赖于 NLog 3.2 的带有 dll 的第二个解决方案,问题是,在将所有内容推送到 GAC(SafeControls 的答案 - 对于 sp 开发人员)之后记录器从未登录过新的解决方案。

        当我再次查看我的 web.config 时铃声响起

        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c" />
        

        注意 Version=2.0.0.0

        然后我知道 NLog 只是加载旧项目的配置,并且在开始使用 3.2 dll 时以某种方式没有传递它。

        我也在尝试重新加载配置,做 DeepCopy,不明白为什么不起作用。

        然后我在代码中重新定义了LoggingConfiguration 以在保存旧配置的同时重新初始化记录器,登录该类,然后在析构函数中重新使用旧配置重新初始化。

        我猜结果是记录器现在到处使用 3.2 和 2.0 配置。

        但后来我讨厌两件事:1 - 有一个析构函数,2 - 没有来自 web.config 的配置,并试图做所有这些没有工作的海峡,并观察到有时我的日志有一些延迟认为可能有一些队列或某些东西我最终找到了正确的方法来制作一个知道适当地重新启动 NLog 的 CTOR:

            public EncryptEventReceiver()
            {
                oldConfig = LogManager.Configuration;
        
                Task t = new Task(() =>
                {
                    FileTarget target = new FileTarget();
                    target.FileName = "c:\\Data\\Logs\\MyProj\\EncryptEventReceiver.txt";
                    target.Layout = "${message}";
                    target.Encoding = Encoding.UTF8;
        
                    LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
        
                    LoggingConfiguration config = new LoggingConfiguration();
                    config.AddTarget("FileLog", target);
                    config.LoggingRules.Add(rule);
        
                    LogManager.Configuration = config;
        
                    var _logger = LogManager.GetCurrentClassLogger();
                    _logger.Trace("EncryptEventReceiver CTOR initialized Logger");
                });
        
                t.Start();
                t.Wait();
        
                LogManager.GetCurrentClassLogger().Trace("EncryptEventReceiver MIDDLE");
                LogManager.Configuration = oldConfig;
                nlog3 = LogManager.GetCurrentClassLogger();
                nlog3.Trace("EncryptEventReceiver CTOR done, nlog3 ready");
            }
        

        编辑: 最后导致一些日志丢失,所以我回到了 CTOR-Destrucor,建议更多地使用 web-config

            public static Logger nlog3;
            private static LoggingConfiguration oldConfig;
        
            public EncryptEventReceiver()
            {
                oldConfig = LogManager.Configuration;
        
                FileTarget target = new FileTarget();
                target.FileName = "c:\\Data\\Logs\\MyProj\\EncryptEventReceiver.txt";
                target.Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}   ${level}   ${message}   ${exception:format=tostring}";
                target.ArchiveAboveSize = 5242880;
                target.ArchiveFileName = "c:\\Data\\Logs\\MyProj\\Archive_EncryptEventReceiver\\EncryptEventReceiver_${shortdate}_{#}.txt";
                target.Encoding = Encoding.UTF8;
        
                LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
        
                LoggingConfiguration config = new LoggingConfiguration();
                config.AddTarget("FileLog", target);
                config.LoggingRules.Add(rule);
        
                LogManager.Configuration = config;
        
                nlog3 = LogManager.GetCurrentClassLogger();
                nlog3.Trace("EncryptEventReceiver CTOR done, nlog3 ready");
            }
        
            ~EncryptEventReceiver()
            {
                nlog3.Trace("EncryptEventReceiver Destructor");
                LogManager.Configuration = oldConfig;
                LogManager.GetCurrentClassLogger().Trace("EncryptEventReceiver Destructor done");
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-31
          • 2013-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多