473,242 Members | 1,483 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,242 software developers and data experts.

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 2349
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
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
by: Paulo Morgado | last post by:
Hi al How do I specify the EventLogEntryType when writing to EventLogTraceListener -- Paulo Morgad Portuga
3
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.