Weird TraceListener NewLine problem

G

Guest

I'm having the darndest problem and I can't seem to recreate it anywhere, and
I'm looking for someone with a little insight.

I've created a custom EmailTraceListener which implements the TraceListener
abstract class. The EmailTraceListener stores all of the messages in a
StringBuilder internally and then writes the StringBuilder.ToString() to an
email as a text message.

Here's the problem I'm encountering. I load up several TraceListeners into
the Trace.Listeners property as a TraceListener[] and then write some
initialization information out to Trace.WriteLine calls, and then I go on my
way and do the normal Tracing. Then I send the email. What I'm seeing is
that the first few line breaks do not appear in my email. Everything is all
on one line. There are four initialization statements that are inserted
using Trace.WriteLines at the beginning, and only the fourth statement is on
its own line. Weird, huh?

What's even weirder is if I try it with just a StringBuilder and the
System.Net.Email classes, I can't recreate it. I can't recreate it with a
TextWriterTraceListener and a StringBuilder and the Email classes either.

Does anyone have any thoughts about why the first few line breaks would be
dropping off?

Thanks very much,

TheManFromSql
 
S

Steven Cheng[MSFT]

Hi TheManFromSql,

From your description, you've written a custom TraceListener which will
write content into a email message's body and send it through the .NET
System.Net.Smtp components. However, you find the format of the email
message has some problem, correct?

As for the custom TraceWriter, would you provide some detailed code logic
about the write message methods? Also, as you mentioned that if you
directly create a StringBuilder and a the SMTP classes to write out the
same text data, they can be displayed well with new lines. If so, are you
using the identical code as the one in custom tracelistener? You can also
save them to a text file to verify the formatting. If you can save the
same text into a file with correct formatting, but failed through email
body, the problem is likely due to the email's body formatting. Also,have
you made sure the email body is of plain text format rather than html?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Steven,

Here is the implementation of the two required TraceListener methods (NOTE:
_emailContent is a StringBuilder object).

#region TraceListener required implementation
public override void Write(string message)
{
lock (this._emailContent)
{
this._emailContent.Append(message);
}
}

public override void WriteLine(string message)
{
lock (this._emailContent)
{
this._emailContent.AppendLine(message);
}
}
#endregion

That's it. For sending the email, I have a lightweight container class that
stores the StringBuilder.ToString() output as the body of the email. This
body then gets passed to the MailMessage class.

In debugging, I have stepped through the communication between the
EmailTraceListener and the MailMessage, and when I inspect the Body, I see
the \r\n's as part of the string. The only problem seems to be that the
first few are dropping off.

Thanks very much,

TheManFromSql
 
S

Steven Cheng[MSFT]

Thanks for your reply,

Since you've ensured that the "\r\n" linefeed are part of the string during
debugging, I think the mail message should also contain the characters. Is
your email format set to html? If so, I suggest you try using "<br/>"
instead of "\r\n" to create new line since HTML does not recognize "\r\n"
as line feed, but use "<br/>" for insert new line.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

The email format is not set to HTML. It is only the first two or three lines
where line breaks are not being used. \r\n line breaks are working further
on in the email.
 
S

Steven Cheng[MSFT]

Thanks for your reply Themanfromsql,

Would you create a simplified project and send it to me? You can reach me
at the email in my signature (by remove "online").

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Themanfromsql,

Thanks for your mail, I've received your attachment of test project and
will have a look and test. I'll update you soon.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Themanfromsql,

I have performed some tests with the sample project you sent and here are
what I got in the local test:

I can correctly write out the trace statements and received the email(in
plain text format) and the email body is as below:

====================
TraceListenerManager instance started listening at 05-28-2007
04:48:54.03202 Created by: TraceListenersTest, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null Connection String: Connection string
goes here.
Machine: GTSC-SC-LTBOX
TWO roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;
Then took the other, as just as fair,
And having perhaps the better claim
Because it was grassy and wanted wear;
Though as for that, the passing there
Had worn them really about the same,
And both that morning equally lay
In leaves no step had trodden black.
Oh, I marked the first for another day!
Yet knowing how way leads on to way
I doubted if I should ever come back.
I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference.
TraceListenerManager instance stopped listening at 05-28-2007
04:48:54.03202 Elapsed time: 00:00:00
=========================

It seems all the "Trace.WriteLine" statement are correctly output in a new
line in the email body. Is this also what you get or should the expected
behavior different?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top