Textwriter problem

  • Thread starter Ronald Moolenaar
  • Start date
R

Ronald Moolenaar

Hi,

My colleague implemented a class overwriting TextWriter. What he tried
to do was logging to the event logger and logging to console. As you
can see in the code below, the part of 'logging to console' will not
work. However, I could not explain the results when I ran the code.

It seems that when I call the first override, i.e. only one parameter
(a string value), the eventlog contains one new entry as I would
expect. When I call the second or third override (format string and
one/more extra arguments), the eventlog contains two new entries! I
can't explain the second entry in the eventlog.

Is this a bug or can anyone explain this? I am using VS.NET 2003.

public WriterLog : TextWriter
{
private EventLog m_log = new EventLog();

public WriterLog()
{
m_log.Source = "Test";
}

public override void WriterLog(string strValue)
{
m_log.WriteLine(strValue);
base.WriteLine(strValue);
}

public override void WriterLog(string strFormat, object arg0)
{
m_log.WriteLine(String.Format(strFormat, arg0));
base.WriteLine(strFormat, arg0);
}

public override void WriterLog(string strFormat, params object[]
args)
{
m_log.WriteLine(String.Format(strFormat, args));
base.WriteLine(strFormat, args);
}
}
 
J

Jon Skeet [C# MVP]

Ronald Moolenaar said:
My colleague implemented a class overwriting TextWriter. What he tried
to do was logging to the event logger and logging to console. As you
can see in the code below, the part of 'logging to console' will not
work. However, I could not explain the results when I ran the code.

It seems that when I call the first override, i.e. only one parameter
(a string value), the eventlog contains one new entry as I would
expect. When I call the second or third override (format string and
one/more extra arguments), the eventlog contains two new entries! I
can't explain the second entry in the eventlog.

Is this a bug or can anyone explain this? I am using VS.NET 2003.

Well, your code won't compile to start with. Please post the code which
*does* compile so we can see what's wrong.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Ronald,

The code you include does not compile, it has several errors:
1- the class keyword is missing
2- You have several methods with the same name that the class "WriterLog" ,
why are they marked as override ? what method of TextWriter are they
overriding?
3- Where are you writting to the console?
4- Did you create the source of the event? Here is some code I got from
MSDN:

if(!EventLog.SourceExists("MySource")){
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
}

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";


Please post the correct code, the one that you compiled and run.

Cheers,
 

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