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 />");
}
}