write to IIS log from an ASP.NET application

I want to have my ASP.NET application write lines to a log somewhere. Does IIS provide any built-in way to log ASP.NET log messages? I was thinking there might be a way to capture calls to System.Diagnostics.Debug.WriteLine(), but I can’t find any way to do it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try Response.AppendToLog(), this writes to the IIS log file.
It’s good because it doesn’t require any additional software. The only downside is that your log file is likely to be huge.
I mainly use it for identifying content logged in the IIS log. Eg. Write the user-name when they login, and you can track their session.

Method 2

To capture the Debug.Write and Debug.WriteLine use the DebugView from sysinternals.

http://technet.microsoft.com/en-us/sysinternals/bb896647

Of course you have to compile with Debug=true or else the functions is not called at all. So the Debug.Write is a good way only for test in real time and debug your application and not a solution to keep log on the errors in general. For the case that you like to save the errors I think that you need to ether use one library of the other user suggestions, or write direct on event viewer your errors.

ps: For some reason the version 4.78 is not working on my windows xp and I switch back to 4.77

Method 3

What I usually do is to use a 3rd-party logging framework and configure the framework through configuration files (i.e. no need to recompile).

The frameworks I have used are:

  • log4net – Which has a lot of so called “appenders” to write to different targets like Windows Event Log or a database or a log file.
  • NLog – A logging framework that is a bit easier to use than log4net.

You have to insert the appropriate log function calls in your application to actually trigger a log entry being created.

Since your question title states that you want to write to the same log (file) as IIS does; I have no idea how to solve this requirement (left alone that I cannot imagine a reason why you would do this)

Method 4

as i know Debug.WriteLine writes to the debug output. You can see it in your debugger and save it from there. It does not write a log file.. Debug Class

  1. Debug Class member’s are Conditional Attritubte See this Answer for explanation

you can try ELMAH ,

is an application-wide error logging facility that is completely
pluggable. It can be dynamically added to a running ASP.NET web
application, or even all ASP.NET web applications on a machine,
without any need for re-compilation or re-deployment.

or you can try Log4Net


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x