473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Weird TraceListener NewLine problem

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
May 9 '07 #1
7 1881
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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

May 10 '07 #2
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

"Steven Cheng[MSFT]" wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

May 11 '07 #3
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.

May 15 '07 #4
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.
May 15 '07 #5
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.
May 18 '07 #6
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.

May 22 '07 #7
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.

May 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daylor | last post by:
hi. i have application with 5 threads. can i use the trace and tracelistener, so if i trace in thread1 , it will write to file1, and if i trace to thread2 if will write to file2. hope im...
0
by: Schorschi | last post by:
Anyone create a custom tracelistener that redirects to a database? Or event to a memory stream rather than a file stream? If an example of a memory stream, when the Write or WriteLine methods...
8
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include...
2
by: John Batdorf | last post by:
Ugh. I obviously don't understand the correct way to use this thing! I have several #if(DEBUG) code blocks to write to a file ErrorLog.txt to troubleshoot why and what exceptions are being...
3
by: Mullin Yu | last post by:
i have a problem with TraceListener. when i close the FileStream, i will have the error: "Cannot access a closed file" but, if i remarks the statement => no closing the FileStream, i will have...
0
by: fd123456 | last post by:
Hi Kevin, You were indeed right. Unfortunately, the ViewState property doesn't change that behaviour. In case it can help someone else, here's the trick I ended up doing. Supposing the variable...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
0
by: bonk | last post by:
I have an application that initializes a lot of tracesources at runtime. The names of these tracesources are only known at runtime. How can I add one tracelistener to all tracesources using the...
4
by: =?Utf-8?B?YXJjaHVsZXRhMzc=?= | last post by:
I want to send trace messages to a textbox in my winforms application using C# 2.0. I have a custom TraceListener class (see below) that is very basic, but I'v got an error implementing it that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.