ASP.NET 中的 System.Diagnostics.Trace.TraceListener UnsafeStringCopy 异常
当我尝试从 ASP.NET 应用程序使用的程序集写入 Diagnostics.Trace 消息时,出现奇怪的间歇性错误。它是 IIS7 上的 .NET 3.5。
代码设置是,当 web.config 指定了 debug 时,我将 HttpResponseTraceListener
的实例添加到 System.Diagnostics.Trace.Listeners
中。我这样做是为了在我的其他程序集中我可以在测试服务器上运行时写出调试信息,而我没有设置使用 Visual Studio 进行调试。因此,在我的另一个程序集中,我使用 System.Diagnostics.Trace.WriteLine 来输出信息,然后该信息也会写入响应流(内联,但就我的目的而言,这很好)。
错误是:
未将对象引用设置为对象的实例。
相关堆栈信息为:
at System.Web.Util.StringUtil.memcpyimpl(Byte* src, Byte* dest, Int32 len) 在System.Web.Util.StringUtil.UnsafeStringCopy(字符串src,Int32 srcIndex,Char [] dest,Int32 destIndex,Int32 len) 在 System.Web.HttpWriter.Write(String s) 在 System.Web.HttpResponse.Write(String s) 在 HttpResponseTraceListener.cs 中的 SSO.HttpResponseTraceListener.Write(String message) 处:第 23 行 在 HttpResponseTraceListener.cs 中的 SSO.HttpResponseTraceListener.WriteLine(字符串消息):第 30 行 在 System.Diagnostics.TraceInternal.WriteLine(String message) 处的
TraceListener 类如下:
public class HttpResponseTraceListener : TraceListener
{
public System.Web.HttpResponse Response { get; private set; }
public HttpResponseTraceListener(System.Web.HttpResponse response)
{
Response = response;
}
public override void Write(string message)
{
if (!string.IsNullOrEmpty(message)
&& null != Response
&& null != Response.OutputStream
&& Response.OutputStream.CanWrite)
{
Response.Write(System.Web.HttpUtility.HtmlEncode(message));
}
}
public override void WriteLine(string message)
{
this.Write(message);
Response.Write("<BR />");
}
}
I get an odd intermittent error when trying to write a Diagnostics.Trace message from an assembly used by an ASP.NET application. It's .NET 3.5 on IIS7.
The code setup is that when web.config has debug specified I add an instance of HttpResponseTraceListener
to System.Diagnostics.Trace.Listeners
. I'm doing it that way so in my other assembly I can write out debug information when running on a test server that I'm not set up to debug using visual studio. So in my other assembly I use System.Diagnostics.Trace.WriteLine
to output information, which then also gets written to the response stream (inline, but for my purpose that is just fine).
The error is:
Object reference not set to an instance of an object.
The relevant stack information is:
at System.Web.Util.StringUtil.memcpyimpl(Byte* src, Byte* dest, Int32 len)
at System.Web.Util.StringUtil.UnsafeStringCopy(String src, Int32 srcIndex, Char[] dest, Int32 destIndex, Int32 len)
at System.Web.HttpWriter.Write(String s)
at System.Web.HttpResponse.Write(String s)
at SSO.HttpResponseTraceListener.Write(String message) in HttpResponseTraceListener.cs:line 23
at SSO.HttpResponseTraceListener.WriteLine(String message) in HttpResponseTraceListener.cs:line 30
at System.Diagnostics.TraceInternal.WriteLine(String message)
The TraceListener class is as follows:
public class HttpResponseTraceListener : TraceListener
{
public System.Web.HttpResponse Response { get; private set; }
public HttpResponseTraceListener(System.Web.HttpResponse response)
{
Response = response;
}
public override void Write(string message)
{
if (!string.IsNullOrEmpty(message)
&& null != Response
&& null != Response.OutputStream
&& Response.OutputStream.CanWrite)
{
Response.Write(System.Web.HttpUtility.HtmlEncode(message));
}
}
public override void WriteLine(string message)
{
this.Write(message);
Response.Write("<BR />");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题在于 TraceListener 从未分离。因此,每次我点击该页面时,我都会添加另一个页面。每次我写入 Diagnostics.Trace 时,它都会写入所有附加的内容。但对于某些响应对象无法写入。
为了解决这个问题,我添加了代码,以便在出现异常时不写入。还添加了代码以在页面加载完成时删除侦听器。
Looks like the problem was that the TraceListeners never were detached. So each time I hit the page I added another one. And each time I wrote to Diagnostics.Trace it would write to all that were attached. But for some the Response object was not able to be written to.
To fix it I added code to not write if there was an exception. And also added code to remove the listener when the page was done loading.