473,396 Members | 1,859 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,396 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 2353
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.