出于各种常见原因,我想对我的ASP.NET应用程序使用跟踪.特别是因为我发现了使用Service Trace Viewer工具的可能性,它允许您以强大的方式检查跟踪.
因为我之前从未使用过这种痕迹,所以我开始研究它.经过一段时间的谷歌,SO和MSDN,我终于明白了事情是如何运作的.但我也发现了一个非常不受欢迎的事情.
在ASP.NET应用程序中使用跟踪时,通过Web请求将跟踪消息组合在一起非常有意义.特别是因为我想使用它的原因之一是研究性能问题.上面提到的工具也通过<Corrleation>在生成的XML文件中使用标记来支持这一点.而这反过来又来自System.Diagnostics.Trace.CorrelationManager.它还允许其他很好的功能,如Activity启动/停止,它提供了更好的跟踪消息分组.很酷,对吗?
我也是如此,直到我开始检查CorrelationManager实际居住的地方.毕竟 - 它是一个静态属性.在玩了一些反射器后,我发现了一些可怕的东西 - 它存储在CallContext!我们不应该在ASP.NET中使用哪种东西,对吧?
所以......我在这里错过了一些东西吗?在ASP.NET中跟踪真的存在根本缺陷吗?
补充:嗯,我有点在自己重写这些东西的边缘.我仍然想使用整洁的工具来探索痕迹.我不应该这样做的原因?也许有更好的东西呢?如果我很快得到答案,那将是非常好的.:)
补充2:我的一位同事证实,这不仅仅是一个理论问题.他在他正在研究的系统中观察到了这一点.所以它已经解决了.我打算建立一个新的小系统,按照我想要的方式做事.:)
补充3:哇,很酷......微软的人们在ASP.NET中使用Correlation Manager时没有发现任何问题.显然我们毕竟没有得到这个bug的修复......
我正在尝试从.NET 3.51启动一个应用程序(操作系统,我的应用程序和我要启动的应用程序都是32位).
启动Process的代码用于其他应用程序,但是有一个令我们头疼的问题.如果我们在应用程序的图标上"双击"它,它会按预期工作,这意味着它可以作为计算机中的应用程序正常工作.直接双击.exe也可以.
操作系统是Windows 7 32位(家庭和/或专业).
我们的.NET应用程序使用x86编译以避免出现问题.
启动"Processes"的代码位于我们制作的DLL(也是32位)内,基本上它是一个简单的DLL,它包含一些"通用代码",我们在整个代码中使用的常用方法,函数和内容.其中一种方法如下所示:
public static bool FireUpProcess( Process process, string path, bool enableRaisingEvents,
ProcessWindowStyle windowStyle, string arguments )
{
if ( process != null )
{
try
{
process.StartInfo.FileName = @path;
if ( arguments != null )
{
if ( arguments != String.Empty )
{
process.StartInfo.Arguments = arguments;
}
}
process.StartInfo.WindowStyle = windowStyle;
process.EnableRaisingEvents = enableRaisingEvents;
process.Start();
}
catch
{
try
{
process.Kill();
}
catch ( InvalidOperationException )
{
} // The process is not …Run Code Online (Sandbox Code Playgroud) 在我的app.config中,我想设置3个跟踪级别(交换机?):详细,警告和无.在代码的调试版本中,我想要在我想要警告的版本中激活详细开关.在特殊情况下,我的应用程序用户可以修改配置文件以禁用所有跟踪.
我希望在控制台上输出调试跟踪,而发布只跟踪到日志文件.
我写了以下内容:
[...]
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="debug" switchName="debug">
<listeners>
<add name="FileLog"/>
<add name="console"/>
</listeners>
</source>
<source name="release" switchName="release">
<listeners>
<add name="FileLog"/>
</listeners>
</source>
<source name="silent" switchName="none">
<listeners/>
</source>
</sources>
<switches>
<add name="debug" value="Verbose"/>
<add name="release" value="Warning"/>
<add name="none" value="Off"/>
</switches>
<!--<sharedListeners>
<add name="FileLog" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="DateTime" initializeData="felix.log"/>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false" />
</sharedListeners>-->
<trace autoflush="false" indentsize="4">
<listeners>
<add name="FileLog" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="DateTime" initializeData="felix.log"/>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
<remove name="Default"/>
</listeners>
</trace> …Run Code Online (Sandbox Code Playgroud) Trace.Listeners和Debug.Listeners共享相同的内部集合,因此我无法向Trace.Listeners添加跟踪侦听器,也无法向Debug.Listeners添加调试侦听器以区分它们.
我怎样才能做到这一点?
编辑:
为什么我要这样做只是因为我正在为我们的应用程序编写一个日志记录层,我希望通过系统来跟踪不同的日志,其中Debug/Trace是两个日志源(还有其他几个来源)我想跟踪.
我正在尝试从我们的内部网站运行应用程序.当我使用时,Process.Start("notepad");我可以看到记事本进程在我们的Web服务器中启动,并在应用程序池设置中提到了默认标识.
但我必须使用特定的用户名和密码启动该过程.所以我试着运行这行代码
string password = "XXXXX";
System.Security.SecureString securePwd = new System.Security.SecureString();
foreach (char c in password)
{
// Append the character to the password.
securePwd.AppendChar(c);
}
Process.Start("notepad", "username", securePwd, "domain");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我甚至没有看到在Web服务器中启动任何记事本进程.执行的代码行,因为当我输入错误的密码时,我可以看到我的网页抛出"错误的用户名或密码"错误.
这与自定义Systems.Diagnostics.TraceListener有关
<system.diagnostics>
<sources>
<source name="SomeTraceSourceName"
switchType="System.Diagnostics.SourceSwitch"
switchName="SomeSwitchName">
<listeners>
<clear />
<add name="CustomListener"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="CustomListener"
type="CustomListener, MyAssembly"
initializeData=""/>
</sharedListeners>
<switches>
<add name="SomeSwitchName" value="4" />
</switches>
</system.diagnostics>
Run Code Online (Sandbox Code Playgroud)
默认跟踪侦听器不会发生这种情况.
我发现了这篇MSDN帖子,但最终它并没有证明有用.
我的Logging设置是TraceSource为每个使用不同的Class.
是否可以配置为所有源写入事件的通配符?
<system.diagnostics>
<sources>
<source name ="wildcard" switchValue="Warning">
<listeners>
<add name="textlog" />
</listeners>
</source>
<source name="MySpecificClass" switchValue="All">
<listeners>
<add name="textlog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="textlog"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="Log.log">
</add>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
Run Code Online (Sandbox Code Playgroud) 我想将AutoFlush属性设置为true,但是我需要通过代码来实现。以编程方式。
我已经找到了如何配置跟踪元素以及跟踪类的AutoFlush属性的方法。
然后,我有以下代码来获取TraceSource:
private static TraceSource GetTraceSource()
{
var ts = new TraceSource("TraceManager")
{
Switch =
{
Level = SourceLevels.All
}
};
ts.Attributes.Add("AutoFlush", "true");
ts.Listeners.Remove("Default");
var file = System.IO.Path.GetTempPath() + @"\MyApplication.log";
var textListener = new TextWriterTraceListener(file)
{
Filter = new EventTypeFilter(SourceLevels.All)
};
ts.Listeners.Add(textListener);
return ts;
}
Run Code Online (Sandbox Code Playgroud)
如何在此代码内将AutoFlush属性设置为true?
谢谢。
在.NET中跟踪中"switch"和"filter"有什么区别?他们似乎以类似的方式工作.
<system.diagnostics>
<trace autoflush="true" indentsize="5">
<listeners>
<add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output1.txt">
</add>
<remove name="Default" />
</listeners>
</trace>
<sources>
<source name="DemoApp" switchName="DemoApp">
<listeners>
<add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output2.txt">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/>
</add>
<remove name="Default" />
</listeners>
</source>
</sources>
<switches>
<add name="DemoApp" value="Error"/>
</switches>
</system.diagnostics>
Run Code Online (Sandbox Code Playgroud) 我有一个控制台应用程序(称为主机),该应用程序使用System.Diagnostics.Process管理多个应用程序。主机控制这些外部进程的启动和停止。
当向控制台发出Ctrl + C(SIGINT)时,主机应安全地终止其他进程。问题在于,在主机能够安全关闭它们之前,其他进程也接收Ctrl + C并立即终止。
我知道Ctrl + C会发给控制台进程树中的每个进程。如果主机仍在启动并正在运行,该如何防止Ctrl + C到达其他进程?我没有参与其他进程的开发,因此无法直接更改其对Ctrl + C的处理。谢谢