C# 在配置中为Tracesource TextWriterTraceListener输出添加自定义路径
因此,我使用Tracesource记录一些错误,而不在用户本地windows文档结构中创建日志文件(类似于C# 在配置中为Tracesource TextWriterTraceListener输出添加自定义路径,c#,path,app-config,relative-path,trace,C#,Path,App Config,Relative Path,Trace,因此,我使用Tracesource记录一些错误,而不在用户本地windows文档结构中创建日志文件(类似于System.Environment.SpecialFolder.LocalApplicationData) 但是,我不知道是否可以在配置文件中执行类似的操作 <system.diagnostics> <trace autoflush="true"/> <sources> <source name="MainSourc
System.Environment.SpecialFolder.LocalApplicationData
)
但是,我不知道是否可以在配置文件中执行类似的操作
<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="MainSource"
switchName="MainSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="LogFileListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="LogFileListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="This is the place the output file goes to"
traceOutputOptions="ProcessId, DateTime, Callstack" />
</sharedListeners>
<switches>
<add name="MainSwitch" value="Verbose" />
</switches>
</system.diagnostics>
initializeData是我认为构造函数的一个参数,是我必须放置自定义路径的地方。配置文件中日志文件的路径是绝对的,不能由任何特殊变量假定 但是,您可以动态创建它,这应该可以解决您的问题
配置文件中日志文件的路径是绝对路径,不能由任何特殊变量假定 但是,您可以动态创建它,这应该可以解决您的问题
下面是我用于选项的示例代码。它可以帮助您理解模式
Configuration exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection diagnosticsSection = exeConfiguration.GetSection("system.diagnostics");
ConfigurationElementCollection switches = diagnosticsSection.ElementInformation
.Properties["switches"]
.Value as ConfigurationElementCollection;
foreach (ConfigurationElement switchElement in switches)
{
Debug.WriteLine("switch: name=" +
switchElement.ElementInformation.Properties["name"].Value +
" value=" +
switchElement.ElementInformation.Properties["value"].Value);
}
下面是我用于选择的示例代码。它可以帮助您理解模式
Configuration exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection diagnosticsSection = exeConfiguration.GetSection("system.diagnostics");
ConfigurationElementCollection switches = diagnosticsSection.ElementInformation
.Properties["switches"]
.Value as ConfigurationElementCollection;
foreach (ConfigurationElement switchElement in switches)
{
Debug.WriteLine("switch: name=" +
switchElement.ElementInformation.Properties["name"].Value +
" value=" +
switchElement.ElementInformation.Properties["value"].Value);
}
下面还将更深入地探讨在Microsoft代码中如何实现相对路径解析:这篇文章中还介绍了一种变通方法。下面还将更深入地探讨在Microsoft代码中如何实现相对路径解析:这篇文章中还介绍了一种变通方法。是否有可能自动实现在手动打开配置文件的情况下配置跟踪(通过配置)-就像在普通.net应用程序中一样?在手动打开配置文件的情况下,是否有可能自动配置跟踪(通过配置)-就像在普通.net应用程序中一样?