This project is read-only.

Documentation for FileTraceListener

Dec 21, 2010 at 9:16 PM

Hi, Happy Christmas.

I am trying to customize my log file message format to suit my need and come across the UkAdcLog project. Quite impressed about  what it could be done based on

.Net framework. Really to me as well, a little bit flexibly for the .Net tracing is what I need.

But could I use FileTraceListener as a replacement for .Net FileTlogTraceListener, with LogFileCreationSchedule, if so, how could I config my config file to doing that.

Any example or documentation for that listener class?

 

Cheers 

Dec 21, 2010 at 9:54 PM

Hi,

Yes - you should be able to do that and much more. I described how it works in this blog post:

http://www.thejoyofcode.com/Dogfooding_Ukadc_Diagnostics.aspx

Apologies, it's not documented in this site yet because it is part of the source tree, but not yet the binaries.

Josh

Dec 21, 2010 at 11:27 PM
Edited Dec 22, 2010 at 1:43 AM

Thank you Josh,

 

If it is not yet in the binaries, will it in the version I downloaded (Currrent 56897)?

I just included the  UkadcDiagnostics.dll file in my reference count as binary?

Thanks for the link. For some reason, I've got log file generated, but no output message. 

I am using:

 

<system.diagnostics>
    <sources>
      <source name = "TestSrc">
        <listeners>
          <add name="LogToFile"/>
        </listeners>
      </source>
      <source name="UKAdcSrc" >
        <listeners>
          <clear /> <!-- this removes the DefaulTraceListener -->
          <add name="UKAdcLogToFile"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="LogToFile"
          type="Microsoft.VisualBasic.Logging.FileLogTraceListener, 
                      Microsoft.VisualBasic, Version=8.0.0.0, 
                      Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
                      processorArchitecture=MSIL"
          initializeData="FileLogWriter"
           location="Custom"
           customLocation = "C:\Logs\"
           logFileCreationSchedule="None"
           traceOutputOptions="DateTime"
                 />
       <add name="UKAdcLogToFile" 
               type="Ukadc.Diagnostics.Listeners.FileTraceListener, Ukadc.Diagnostics"
               output="{Message}"
               filePath="C:\Logs\{Year}-{Month}-{Day}.txt" 
            />
    </sharedListeners>
  </system.diagnostics>

 

and

 

private static readonly TraceSource myUkAdcTS = new TracwSource("UkAdcSrc");

myUkAdcTS.TraceEvent(TraceEventType.Information, 1, "From extended source form loaded");

 

for output log line

 

Dec 22, 2010 at 4:43 PM

If the FileListener is in the dll (use reflector to check) you have it. I would recommend building from latest source though as there were a few bug fixes added to the most recent stuff a while back.

If you do have it, there could be ANY number of reasons (usually misconfiguration) why it isn't logging. Check the output window in VS (when debugging or dbgview when not) as that's where we write to when something goes wrong for us.

If you still can't get it to work, let me have a repro (upload to SkyDrive or similar).

Josh

Dec 23, 2010 at 12:58 AM

Hi Josh,

 

The FileTraceListener should be in the dll file, as It is supposed to be the latest build, and I am using the same dll for a testing project and it works with FileTraceListener.

Also I did add two listeners: FileLogTraceListener and OutputDebugStringTraceListener under the same TraceSouce instance and they both seems works (Output event lines).

It seems just the FileTranceListener doesn't work in my application, I suspect it is a config issue?

 

  <sources>
      <source name="UKAdcSrc" switchValue="All">
        <listeners>
          <add name="UKAdcLogToFile"
              type="Ukadc.Diagnostics.Listeners.FileTraceListener, Ukadc.Diagnostics"
              output="{EventType} - {Message}"
              filePath="C:\Logs\UKSoapLog-{Year}-{Month}-{Day}.txt" />
             <add name="LogToFile"
          type="Microsoft.VisualBasic.Logging.FileLogTraceListener, 
                      Microsoft.VisualBasic, Version=8.0.0.0, 
                      Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
                      processorArchitecture=MSIL"
          initializeData="FileLogWriter"
           location="Custom"
           customLocation = "C:\Logs"
           logFileCreationSchedule="Daily"
           traceOutputOptions="DateTime" />
          <add name="ods" type="Ukadc.Diagnostics.Listeners.OutputDebugStringTraceListener, Ukadc.Diagnostics" 
					initializeData="{EventType}: {Message} - {DateTime}, Process={ProcessId}, Thread={ThreadId}" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

 

and again, I just instance an static TraceSource  and call the TraceEvent method to output log infor:

 

private static TraceSource myUts = new TraceSource("UKAdcSrc");

myUts.TraceEvent(TraceEventType.Information, 0, "From Ukadc source, form loaded");

I did try a simple project using FileTraceListener and it works, but I couldn't see what's different when I am trying to use it in my existing project?

Dec 23, 2010 at 1:15 AM
Edited Dec 23, 2010 at 1:16 AM

Also not sure related or not, I got the following message from Debug output

"The cleanInterval attribute must be a valid TimeSpan. The value '' is invalid."

I thought I don't need interval attribute for the FileTraceListener?

Dec 23, 2010 at 4:49 AM

Yes, definitely related. Sounds like a bug too - that should default to 1 hour. It's how often the system checks for loose unuse but 'open' files. Because of the nature of the way we roll to new files, we can't know whether an existing file will be reused again or not (or at least, it would be very tricky). Therefore we clean up (close the connection to) files if they haven't been written to in the last cleanInterval period.

To get you through this problem, you could just add a cleanInterval="01:00:00" attribute to your FileTraceListener. Sorry about the bug - I'll add it to the backlog, not sure when I'll get to it but hopefully soon.

Josh

Dec 23, 2010 at 5:10 AM

Hi, this wasn't a bug after all. I just changed the debug output to be clearer (if it fails to parse a cleanInterval it defaults to 1 hour and tells you so).

So I'm not sure what is going wrong for you. Can you share a small repro? I'll send you my mail address.

Thanks

Josh

 

 

Dec 23, 2010 at 5:36 AM
Edited Dec 23, 2010 at 5:43 AM

Wait! Did you forget the TraceSwitch value - looking at the config at the top you need this (the emboldened section is new)

 

<system.diagnostics>
    <sources>
      <source name = "TestSrc" switchValue="All">
        <listeners>
          <add name="LogToFile"/>
        </listeners>
      </source>
      <source name="UKAdcSrc" switchValue="All">
        <listeners>
          <clear /> <!-- this removes the DefaulTraceListener -->
          <add name="UKAdcLogToFile"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="LogToFile"
          type="Microsoft.VisualBasic.Logging.FileLogTraceListener, 
                      Microsoft.VisualBasic, Version=8.0.0.0, 
                      Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
                      processorArchitecture=MSIL"
          initializeData="FileLogWriter"
           location="Custom"
           customLocation = "C:\Logs\"
           logFileCreationSchedule="None"
           traceOutputOptions="DateTime"
                 />
       <add name="UKAdcLogToFile" 
               type="Ukadc.Diagnostics.Listeners.FileTraceListener, Ukadc.Diagnostics"
               output="{Message}"
               filePath="C:\Logs\{Year}-{Month}-{Day}.txt" 
            />
    </sharedListeners>
  </system.diagnostics>

HTH

Josh

Dec 23, 2010 at 5:40 AM
Edited Dec 23, 2010 at 7:33 AM

Hi Josh, 

Yeah, I confirmed that the cleanInterval attribute didn't work.

The thing is that in my testing small project, it is working. but not in production application. The application is a prototype anyway without any important data. I will try to

doing some clean up and setup a repro for you tomorrow, to late for me to work now.

Thanks again.

Dec 23, 2010 at 8:30 AM

Hi, in the new version in my post , I do have switchValue set,  and I tried

private static TraceSource myUts = new TraceSource("UkAdcSrc", SourceLevels.All);
as well, doesn't seem make any difference.

Dec 26, 2010 at 8:56 AM

This is a very strange problem - it all stems from the fact that the FileStream's Buffer to which the Listener writes to isn't being flushed.

If you write a lot of event data (call .TraceData a thousand times or more) you'll see the buffer does get flushed to the file. To workaround this problem the System.Diagnostics folk created a <trace autoflush="true" /> setting that can be applied to configuration.

When this is true, the Writer should automatically flush any buffers with each write. We have the appropriate code in the FileTraceListener.

if (Trace.AutoFlush)
{
   sw.Flush();
}

However, for some reason I can't fathom - Trace.AutoFlush is always false when I test with your sample repro. We accessed Trace.AutoFlush directly (rather than overriding Flush) for performance reasons. Sadly, I've had to remove this optimisation. If you get the latest code, it should work fine now. (remember to set <trace autoflush="true"/> if thats the behavior you want.

Thanks for your help with this - I think this is a good find! Let me know how you get on.

Ta

Josh

Dec 26, 2010 at 8:57 AM
Edited Dec 26, 2010 at 8:58 AM

PS - Remember this project is a hobby and without warranty (especially this pre-binary stuff!).

Jan 4, 2011 at 8:20 PM

Hi Josh,

In fact, I did have one instance where when my application write a lot messages to log file, the fileTracelistener loged some message.

And set autoflush to true did solve my problem, thank you for find that out for me.