Sunday, February 13, 2011

Could someone give me a "hello world" example of logging in ASP.NET?

I'm trying to get a bare-bones example of logging going in my ASP.NET application. I would like to use the My.Log functionality to write out error log messages to a text log file. I have tried several things via Google but none of them seem to work. In general, when I use any of the properties of My.Log.DefaultFileWriter in the code it says "Object reference not set".

My basic question is: What do I need in my web.config file (and/or anywhere else, if necessary) so that I can write messages with

My.Log.WriteEntry("blahblahblah")

in my code, to a text file, D:\log.txt?

Thanks.


Edit: specific code used:

    <system.diagnostics>
    <sources >
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>    
      <add name="FileLog" 
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"    
           initializeData="FileLogWriter" />
    </sharedListeners>
    <switches>
      <add name="DefaultSwitch" value="Verbose"/>
    </switches>
    <trace autoflush="true"></trace>
  </system.diagnostics>

Then in the code:

My.Log.DefaultFileLogWriter.CustomLocation = "D:\"
My.Log.DefaultFileLogWriter.BaseFileName = "log"
My.Log.WriteEntry("blahblahblah")

(this would write to D:\log.log).

0 comments:

Post a Comment