473,508 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EventLogTraceListener incompatible with typical usage of Trace?

Ken
I would like to start using EventLogTraceListener, and am running into
a couple of significant limitations:

1) I have found that there is no way to write EventLog entries with
different EventLogEntryTypes. It seems that EventLogTraceListener is
only capable of writing entries with EventLogEventType.Informat*ion,
even for Trace.Fail. Is this assessment correct?

2) EventLogTraceListener.Flush (and the idea of buffering writes) is
not applicable to EventLogTraceListener. It seems that each call to
Trace.Write() or Trace.WriteLine() results in the creation of a new
EventLog entry.

This behavior is undesirable and incompatible with my usage of
Trace.Write() and Trace.WriteLine(). For example:

try
{
// Remove the DefaultTraceListener
Trace.Listeners.Clear();

// Create EventLogTraceListener with an EventSource of "Test"
EventLogTraceListener evListener = new EventLogTraceListener("Test");
// Register the listener
Trace.Listeners.Add(evListener);

// Raise an exception
throw new Exception("Fake exception");
}
catch(Exception ex)
{
// Construct trace output message with date and exception info
Trace.Write(System.DateTime.No*w.ToString());
Trace.Write(ex.GetType().Name)*;
Trace.Write(ex.Message);
Trace.Write("\r\n");
Trace.WriteLine(ex.StackTrace)*;
}

The code above uses Write() and WriteLine() in the way that I believe
they were intended, based on the presence of other formatting methods
for intentation. This use of Trace works great for file logging.

If I add the EventLogTraceListener, however, the above code
will create five entries in the EventViewer, no matter how I use
Trace.AutoFlush and Trace.Flush(). This means that
EventLogTraceListener is essentially unusable for me if I have
widespread usage of Trace like that above.

Subclassing TraceListener to create a listener that writes "correctly"
to the EventViewer is not a viable option because the Trace class has
no members that would help to group a series of Write()/WriteLine()
calls into a a single logical log entry.

So ultimately, it is not possible to use Trace's
Write()/WriteLine()/buffering capabilities (standard for logging to
files and console) AND create meaningful EventViewer entries. This
seems like an oversight in the design of the Trace API.

Please comment; I would gladly be proven wrong on either point,
especially #2.

Ken

Jul 22 '05 #1
2 2362
Tracing works fine for both cases, but you have to remember that each write
operation is a single entry , where ever this entry goes, file or eventlog.

For event log if you want a single entry, simply build your complete string
message before wrtiting it. This is the way I use it

Don't forget also that EventLogTraceLisner send EvenLog object.
You coudl you also directly the EventLog class

HOpe it helps

"Ken" wrote:
I would like to start using EventLogTraceListener, and am running into
a couple of significant limitations:

1) I have found that there is no way to write EventLog entries with
different EventLogEntryTypes. It seems that EventLogTraceListener is
only capable of writing entries with EventLogEventType.InformatÂ*ion,
even for Trace.Fail. Is this assessment correct?

2) EventLogTraceListener.Flush (and the idea of buffering writes) is
not applicable to EventLogTraceListener. It seems that each call to
Trace.Write() or Trace.WriteLine() results in the creation of a new
EventLog entry.

This behavior is undesirable and incompatible with my usage of
Trace.Write() and Trace.WriteLine(). For example:

try
{
// Remove the DefaultTraceListener
Trace.Listeners.Clear();

// Create EventLogTraceListener with an EventSource of "Test"
EventLogTraceListener evListener = new EventLogTraceListener("Test");
// Register the listener
Trace.Listeners.Add(evListener);

// Raise an exception
throw new Exception("Fake exception");
}
catch(Exception ex)
{
// Construct trace output message with date and exception info
Trace.Write(System.DateTime.NoÂ*w.ToString());
Trace.Write(ex.GetType().Name)Â*;
Trace.Write(ex.Message);
Trace.Write("\r\n");
Trace.WriteLine(ex.StackTrace)Â*;
}

The code above uses Write() and WriteLine() in the way that I believe
they were intended, based on the presence of other formatting methods
for intentation. This use of Trace works great for file logging.

If I add the EventLogTraceListener, however, the above code
will create five entries in the EventViewer, no matter how I use
Trace.AutoFlush and Trace.Flush(). This means that
EventLogTraceListener is essentially unusable for me if I have
widespread usage of Trace like that above.

Subclassing TraceListener to create a listener that writes "correctly"
to the EventViewer is not a viable option because the Trace class has
no members that would help to group a series of Write()/WriteLine()
calls into a a single logical log entry.

So ultimately, it is not possible to use Trace's
Write()/WriteLine()/buffering capabilities (standard for logging to
files and console) AND create meaningful EventViewer entries. This
seems like an oversight in the design of the Trace API.

Please comment; I would gladly be proven wrong on either point,
especially #2.

Ken

Jul 22 '05 #2
Ken
Serge,

I understand what you're saying and agree that constructing the full
trace message prior to calling Trace.WriteLine() will get me what I
want.

I would submit, however, that the Trace's interface suggest an intended
usage much like a StreamWriter. This allows you to conveniently write
Trace information out to whatever persistence store you want. This
makes perfect sense for logging to a file or console (which is what
most of the examples illustrate), but it doesn't work so well with
stores that have a concept of a "record" like the eventlog or a
database. A critically important feature of Trace is that you can
dynamically determine who the listeners are, and Trace never needs to
care about who the listeners are. If you have to use Trace differently
depending on who is listening, you're taking away this key feature.

Ken

Jul 22 '05 #3

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

Similar topics

7
8049
by: Stan | last post by:
According to MSDN the following config files settings provide send tracing information to event log: <add name="MyListener" <add name="MyEventListener"...
4
2331
by: Paulo Morgado | last post by:
Hi al How do I specify the EventLogEntryType when writing to EventLogTraceListener -- Paulo Morgad Portuga
3
17751
by: Brian Stubblefield | last post by:
Dear clc members, I am rather new to the C programming language. I have a rather large program that I am currently debugging. Currently, the following snippet of code in the c program: ...
0
1627
by: Joerg Schoen | last post by:
Hi folks! "JSCPP" is a ANSI-C preprocessor and language parser. It can analyze or instrument C-code. The most prominent application currently is to add function trace calls to existing C...
26
10820
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
0
1292
by: julien | last post by:
Hi, I have the following listeners in the web.config: <add name="FileTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\CapriMon\CapriMon.log"/> <add...
2
309
by: Ken | last post by:
I would like to start using EventLogTraceListener, and am running into a couple of significant limitations: 1) I have found that there is no way to write EventLog entries with different...
4
2208
by: duduch_1er | last post by:
Hello i tried this code in my stylesheet : xsl:variable name="items" select="xsl-usexsl:getElementsBySplitString(.,'i')" /> whith the java function getElementsBySplitString here the code : ...
0
4908
by: srikar | last post by:
Hi all, I am having a problem, when I am compiling the code in 32 bit option on a 64 bit machine using the macro CPPFLAGS= -m32 I am getting the following warnings from the linker . ...
0
7225
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
7123
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
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
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
7498
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...
1
5053
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.