Writing Exceptions to the Application Log

G

Guest

Hi there

I'm trying to find the correct way to write exceptions to the Application
log using Vb.Net.

I've done a fair bit of Googleing and although this gets plenty of mentions
and that this is apparently the correct way forward nobody offers a solution
as to how it is actually done.

I gather that code like:

Try
'my code goes here
Catch ex As Exception
My.Log.WriteException(ex)
End Try

Will write the exception to a listener but there is no mention of how a
listener is set up on a web App and how this is configured to write to the
Application Log (Security permissions etc)

Plenty of the on line info suggest a Registry hack but this doesnt seem
appropiatte.

PS. I do find it ironic that if i dont catch the exception it gets written
to the log by default.

Thanks for your help.

Martyn
 
J

Juan T. Llibre

Ahh...
Thanks for that update, Peter.




Peter Bromberg said:
Thanks to Juan for the mention, actually I have an even more "Generic"
implementation of that here:

http://www.eggheadcafe.com/articles/20060531.asp

However, if you really want to write to the Event Log, take a look at the
System.Diagnostics Namepace. There are static methods there for this.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
S

Steven Cheng[MSFT]

Thanks for Juan and Peter's informative resource.

Hi Martyn,

As for using the "My.Log" object (VB.NET specific ) to log information in
ASP.NET 2.0 web application, you need to do the following configuration:

1. First register a text file listener in the web.config if you want to
write the trace log into text file. e.g.

===========
<system.diagnostics>
..................
<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\web_app_log.txt"/>

</sharedListeners>
</system.diagnostics>
===========

Be careful that we should not use the
"Microsoft.VisualBasic.Logging.FileLogTraceListener" class here because the
"Microsoft.VisualBasic.Logging.FileLogTraceListener" class will always look
for a path in the current user's Local application directory( and this path
doesn't exist for the ASP.NET service account, "Network Service" or
"machine\ASPNET"). Therefore, we use the
System.Diagnostics.TextWriterTraceListener class instead(if you want, you
can use some other XML Trace Listeners under that namespace too)


2. Add a switch under the <switches> section like:

===========
<system.diagnostics >
.............
<switches >
<add name="DefaultSwitch" value="Verbose" />
</switches>
..............
============

This is used to control what level information will be logged(and other
level information will be ignored)

#Configuring Trace Switches
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconconfigurationoftrace
switches.asp?frame=true


3. After add the Listener and Switch elements, we need to add the "Source"
element for our My.Log object.
==================
<system.diagnostics >
.............................
<sources >
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>
</listeners>
</source>

..................
=================

This "DefaultSource" will be used by our VB.NET My.Log object and we
reference the "FileLogListener" configured previously.

In addition, we should add the <trace autoflush="true" ></trace> to make
the runtime immediately write out trace after we call My.Log.Writexxxx


So the complete configuration section will look like below:


===============================
<system.diagnostics >
<trace autoflush="true" ></trace>
<sources >
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>

</listeners>
</source>
</sources>

<switches >
<add name="DefaultSwitch" value="Verbose" />
</switches>

<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\web_app_log.txt"/>


</sharedListeners>
</system.diagnostics>
===================================

You can put it in your application's web.config file and use My.Log object
to write trace log. BTW, make sure your ASP.NET servcie identity has
sufficient permission to modify log files in the certain path we specified
in the above <sharedListeners>. like:

==============
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\web_app_log.txt"/>
==============



BTW, the following msdn document describes most information about
configuration of My.Log object( and add custom listeners for it). However,
there is one error in it. We should not use the
"Microsoft.VisualBasic.Logging.FileLogTraceListener" class for ASP.NET
application as I mentioned above, just take care of this.

#Walkthrough: Changing Where My.Application.Log Writes Information
http://msdn2.microsoft.com/en-us/library/5cz98azz.aspx

If you have anything unclear or any further questions, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks for all the responses, I'll take a look through and see what suits the
situation best.

I have to say the solution for "Dummies" sounds appealing!
--
Regards

Martyn Fewtrell
 
G

Guest

Once again thanks for all the replies.

Steven

The information you have sent me is great apart from the fact that I do
actually want to write the information to the application log. I appreciate
the principal follows through the same logic. Therefore I assume the line:

<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\web_app_log.txt"/>

</sharedListeners>

needs to be something different? Something like:

<sharedListeners>
<add name="ApplicationLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="something different in here?"/>

</sharedListeners>

Note the "Something different in here?"

After this I think the real issue is the comment:

"make sure your ASP.NET service identity has sufficient permission to modify
log files in the certain path we specified"

which is fine for a folder on the local drive where I can give access
(Read/Write etc) for the Network Service account but what I don’t know is
what user writes does which account need to write to the Application Log and
where do I set this.

Again I assume this is the Network Service account on ASP.Net 2.0 with Win
Server 2003 (IIS6) but what writes does it need and how are they set?

Thanks again for your comprehensive answer.

--
Regards

Martyn Fewtrell
 
S

Steven Cheng[MSFT]

Hi Martyn,

Just found that I've missed the point that you're wantting to write the
error message into "Event Log" (I previously think that the "Application
Log" you mentioned means a custom application log file). If so, you should
replace the text file listener with an EventLog Trace Listener class.
The "System.Diagnostics.EventLogTraceListener" class is a built-in class
which is capable of this. Here is a modified configuratino section that use
both text file and eventlog trace listener:

===============================
<system.diagnostics >
<trace autoflush="true" ></trace>
<sources >
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>
<add name="EventLogListener" />
</listeners>
</source>
</sources>

<switches >
<add name="DefaultSwitch" value="Verbose" />
</switches>

<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\web_app_log.txt"/>



<add name="EventLogListener"
type="System.Diagnostics.EventLogTraceListener,
System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="Application"/>
</sharedListeners>
</system.diagnostics>
===================================

also, take care of the "initializeData" attribute in the eventlog listener
configuration element.

=============
<add name="EventLogListener"
.............................
initializeData="Application"/>
</sharedListeners>
=====================

Here I set it to "Application" because ASP.NET worker process identity(such
as Network Service) can only write to the "Application" source in the
eventlog, if you change to other value, that means you would write to a
"Custom Event Source". Then you may get "Access Denied" error, in such
cases, you need to grant the ASP.NET process identity sufficient permission
to write into that custom event source (of course you need to create the
custom event source first if it doesn't exist originally).

Hope this also helps. If you have anything unclear, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 
G

Guest

Thanks Steven

I have managed to write to a text file on the hard drive based on your
earlier post so that bit worked fine. I'll try these later modifications and
get back to you if I get stuck.

Once again thanks!
 
G

Guest

Steven

Thats great my Test Application works fine so I can now transfer it across
to the real project. I guess I need myself a good book on Web Config files
and what can and can't be added etc.
 
S

Steven Cheng[MSFT]

Thanks for your quick reply,

Seems you are much quicker than I have expected since I'm just trying to
reply your another previous message. However, you have already got it
working now :).

As you mentioned that you'll transfer the test project to your real
project(is it in another machine and environment?), I suggest you make
further check to ensure that your current test application's enviornment
setting is mostly identitcal to the real one's. Such as the security
context/identity that runs the application. This can help avoid some
unexpected erros when move to the target place.

As always, if you meet any further problems on this, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 
S

Steven Cheng[MSFT]

Thanks for your input Alvin,

Yes, the enterprise library's exception handling block has much improved in
2.0.

BTW, I've also read Peter's article and found that his logging component is
quite similar with EntLib's exception handling block , at least from the
syntax :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top