企业日志记录不转换 XML 跟踪侦听器文件名规范中的环境变量

发布于 2025-01-01 15:04:47 字数 1658 浏览 3 评论 0原文

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

  <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>

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

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

我将收到带有文本的 DirectoryNotFoundException:无法找到路径的一部分 < code>'D:\Project\Application\bin\x86\Debug\%ALLUSERSPROFILE%\CompanyName\AppName\Diagnostics\ErrorLog.xml'.

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

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

这是我第一次使用企业日志记录应用程序块,因此我不知道版本之间的行为是否发生了变化,以及这个版本是否有这些怪癖。目前,我已将路径硬编码到我的 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);

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'.

When I use an absolute path like 'C:\ProgramData\CompanyName\AppName\Diagnostics\ErrorLog.xml' it works.

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.

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.

Suggestions appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2025-01-08 15:04:47

我希望在今天早上回去工作之前看到有人回答这个问题。看到没有答案,我想我会再花几分钟来解决这个问题,因为我只有一个臭气熏天的解决方法。

我安装了该库提供的源代码(谢谢 Microsoft),构建了解决方案并检查了代码。简而言之,环境变量的扩展只会发生在提供给 FlatFileTraceListener 的文件名中。虽然预期 XmlTraceListener 基于文件的使用会执行相同的操作似乎是合理的,但事实并非如此。如果不扩展环境变量,您将看到我上面描述的行为。

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

希望这可以节省其他人几个小时的时间。

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.

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.

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.

咽泪装欢 2025-01-08 15:04:47

“在文件名中使用环境变量时不会扩展”的问题可以通过将最新的 entlib 版本 5.0 添加到您的解决方案中来解决。我通过以下方式下载它:

  1. 右键单击您的解决方案(VS 2013)。
  2. 管理 NuGET 包。
  3. 搜索 entlibcontrib 5.0 日志记录扩展块。
  4. 将其添加到您的所有项目中。

然后,日志记录类能够解析文件名中的 %USERPPROFILE% 字符串,并且我能够看到在 userprofile 文件夹中创建我的日志文件。

The issue of "environment variables are not expanded when used in filenames" can be solved by adding the latest entlib version 5.0 to your solution. I downloaded it by:

  1. Right click on your solution (VS 2013).
  2. Manage NuGET Packages.
  3. Search for entlibcontrib 5.0 logging extension block.
  4. Add it to all your projects.

Then the logging class was able to parse the %USERPPROFILE% string in the file name and I was able to see my log file being created in the userprofile folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文