2015-10-16 67 views
2

.NET TraceListener如何配置爲登錄TEMP文件夾? 是否有可能做到這一點的app.config像:.NET TraceListener如何配置爲登錄TEMP文件夾

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

..不做任何更改代碼?

或者只有在代碼中創建偵聽器時纔可以這樣做?

回答

0

你必須在你的代碼中設置它。

正如您在source code中所看到的,構造函數立即分配給文件名,而不分析文件名的內部結構。

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

再有就是一些代碼來處理的可能性,該文件已被鎖定,但沒有什麼應對的可能性,該文件夾是不可寫(TEMP通常是不得已而爲之的可寫文件夾)

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); 
相關問題