如何将.NET TraceListener配置为登录到TEMP文件夹 [英] How can .NET TraceListener be configured to log to TEMP folder

查看:161
本文介绍了如何将.NET TraceListener配置为登录到TEMP文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET TraceListener如何配置为登录到TEMP文件夹? 是否可以在app.config中执行以下操作:

How can .NET TraceListener be configured to log to TEMP folder? Is it possible to do this in app.config like:

      <add type="System.Diagnostics.TextWriterTraceListener" 
          initializeData="%Temp%\logfilename.log"/>

..没有进行任何代码更改?

.. without making any code changes?

还是仅当您使用代码创建侦听器时才能这样做?

Or can this only be done if you create your listener in code?

推荐答案

您必须在代码中进行设置.

You'd have to set it in your code.

您可以在

As you can see in the source code, the constructor immediately assigns to file name with no parsing of the internal structure of the file name.

 public TextWriterTraceListener(string fileName, string name) : base(name) {
            this.fileName = fileName; 
        } 

然后有一些代码来处理文件已被锁定的可能性,但是没有办法处理该文件夹不可写的可能性(TEMP通常是最后一个可写的文件夹)

And then there is some code to deal with the possibility that the file is already locked, but nothing to deal with the possibility that the folder isn't writable (TEMP is usually a writable folder of last resort)

    string fullPath = Path.GetFullPath(fileName);
                    string dirPath = Path.GetDirectoryName(fullPath); 
                    string fileNameOnly = Path.GetFileName(fullPath);
//more code
       fileNameOnly = Guid.NewGuid().ToString() + fileNameOnly;
                        fullPath = Path.Combine(dirPath, fileNameOnly); 

这篇关于如何将.NET TraceListener配置为登录到TEMP文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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