企业日志记录未转换XML跟踪侦听器fileName规范中的环境变量 [英] Enterprise Logging not translating environment variables in XML Trace Listener fileName specification

查看:124
本文介绍了企业日志记录未转换XML跟踪侦听器fileName规范中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Enterprise Library 5.0可选更新1进行日志记录。我的app.config文件中有一个声明的部分,如下所示:

I am using Microsoft Enterprise Library 5.0 Optional Update 1 for logging. I've got a declared section in my app.config file as follows:

  <loggingConfiguration name="LLamasoftLoggingConfiguration" tracingEnabled="true"       defaultCategory="General">
        <listeners>
          <add name="XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              fileName="%ALLUSERSPROFILE%\CompanyName\AppName\Diagnostics\ErrorLog.xml" traceOutputOptions="None" />
        </listeners>
    ...
  </loggingConfiguration>

当我运行应用程序并开始使用日志记录时,例如,

When I run the application and get down to using the logging, e.g.,

  logWriter = Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
  logWriter.Write(logEntry);

我将收到一个DirectoryNotFoundException文本:找不到路径<$ c的一部分$ c>'D:\Project\Application\bin\x86\Debug\%ALLUSERSPROFILE%\CompanyName\AppName\Diagnostics\ErrorLog.xml'。

I'll get a DirectoryNotFoundException with the text: Could not find a part of the path 'D:\Project\Application\bin\x86\Debug\%ALLUSERSPROFILE%\CompanyName\AppName\Diagnostics\ErrorLog.xml'.

当我使用'C:\ProgramData\CompanyName\AppName\Diagnostics\ErrorLog.xml'之类的绝对路径时有效。

我看到许多参考资料说我应该能够在日志文件路径中使用环境变量,但是无论我尝试哪种方式,它们将附加到当前执行目录路径中。参考文献还指出,如果该文件夹不存在,则日志记录系统将创建该文件夹,但是我必须确保该文件夹存在。

I see a number of references saying I should be able to use environment vars in the log file path but, no matter which I try, they get appended to the current execution directory path. The references also say that the logging system will create the folder if it does not exist but I have to ensure that it exists.

这是我第一次使用Enterprise Logging应用程序块,所以我不知道版本之间的行为是否已更改,以及是否只有这些怪癖。现在,我已经将路径硬编码到了我的app.config文件中,但对于最终版本,应该动态确定。

This is my first use of the Enterprise Logging application block so I don't know whether the behavior has changed between versions and if this one just has these quirks. For now, I've hard-coded the path into my app.config file but for the final version this should be dynamically determined.

建议建议。

推荐答案

我希望看到有人在我今天早上恢复工作之前回答了这个问题。没找到答案,我想我将只花几分钟的时间在问题上,因为我只有一个臭味的解决方法。

I was hoping to see that someone answered the question before I got back into work this morning. Seeing no answer, I thought I would spend just a couple of minutes more on the problem since I had only a smelly workaround in place.

我安装了随附的源代码库(感谢Microsoft),构建了解决方案并检查了代码。简短的答案是,仅使用提供给FlatFileTraceListener的文件名进行环境变量的扩展。可以合理预期XmlTraceListener的基于文件的用法会做同样的事情,但事实并非如此。在不扩展环境变量的情况下,您将看到如上所述的行为。

I installed the source code supplied with the library (thank you, Microsoft), built the solution and examined the code. The short answer is, the expansion of the environment variables will only occur with the file names provided to the FlatFileTraceListener. While it seems reasonable to expect that the file-based usage of the XmlTraceListener would do the same thing, it does not. Without the expansion of the environment variables, you'll see the behavior as I described above.

如果要更改行为,请查看Logging.2010项目并检查TraceListeners文件夹中的FlatFileTraceListener.cs和XmlTraceListener.cs代码。您会看到一个对帮助器的调用, EnvironmentHelper.ReplaceEnvironmentVariables(string fileName)与平面文件实现而不是xml(文件)实现一起使用。

If you want to change the behavior, look into the Logging.2010 project and examine the FlatFileTraceListener.cs and XmlTraceListener.cs code in the TraceListeners folder. You'll see a call to a helper, EnvironmentHelper.ReplaceEnvironmentVariables(string fileName), used with the flat file implementation and not with the xml (file) implementation. It looks to be an easy change.

希望可以节省别人几个小时的时间。

Hope that saves somebody else a few hours of time.

这篇关于企业日志记录未转换XML跟踪侦听器fileName规范中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆