Textwriter problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronald Moolenaar

    #1

    Textwriter problem

    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(strin g strValue)
    {
    m_log.WriteLine (strValue);
    base.WriteLine( strValue);
    }

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

    public override void WriterLog(strin g strFormat, params object[]
    args)
    {
    m_log.WriteLine (String.Format( strFormat, args));
    base.WriteLine( strFormat, args);
    }
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Textwriter problem

    Ronald Moolenaar <rmoolenaar@dol fijn.nl> wrote:[color=blue]
    > 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.[/color]

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

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

      #3
      Re: Textwriter problem

      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.So urceExists("MyS ource")){
      EventLog.Create EventSource("My Source", "MyNewLog") ;
      Console.WriteLi ne("CreatingEve ntSource");
      }

      // 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,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "Ronald Moolenaar" <rmoolenaar@dol fijn.nl> wrote in message
      news:37e83d6c.0 311050409.211b5 dc9@posting.goo gle.com...[color=blue]
      > 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(strin g strValue)
      > {
      > m_log.WriteLine (strValue);
      > base.WriteLine( strValue);
      > }
      >
      > public override void WriterLog(strin g strFormat, object arg0)
      > {
      > m_log.WriteLine (String.Format( strFormat, arg0));
      > base.WriteLine( strFormat, arg0);
      > }
      >
      > public override void WriterLog(strin g strFormat, params object[]
      > args)
      > {
      > m_log.WriteLine (String.Format( strFormat, args));
      > base.WriteLine( strFormat, args);
      > }
      > }[/color]


      Comment

      Working...