views:

514

answers:

3

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).

+3  A: 

Walkthrough: Changing Where My.Application.Log Writes Information

I'd already been to that page, and I swear I tried it before, but I tried it again and this time it worked! Thanks.
Mark
+2  A: 

I know it's not a hello world example, but I suggest also checking out log4net (http://logging.apache.org/log4net/index.html). It's pretty awesome.

jonnii
+1 for log4net. Very configurable and nice to use.
rslite
I was trying to stick with the built-in stuff for this project, but if I need something with more features I'll check it out.
Mark
A: 

log4net is the bst option for logging in asp.net

renegadeMind