Discussion:
logging not possible in finalizer on application shutdown?
Michael Hinkel
2006-02-24 11:03:31 UTC
Permalink
All,

I'd like to log some information in a

protected virtual void Dispose(bool disposing)

method which can be called either by Dispose() or by the finalizer
(implemented according to Microsoft's proposal "Implementing a Dispose
Method"; see
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconimpleme
ntingdisposemethod.htm).

If Dispose(bool disposing) is called by Dispose() everything works fine,
but if the method is calld by the finalizer on application shutdown, no
log entry is created. If I use a Console.WriteLine(message) the message
is sent to the Console even on application shutdown.

Any suggestions?


Michael

_______________________________________
DATATRAK Deutschland GmbH
Michael Hinkel
Software Developer
Rochusstrasse 65
53123 Bonn

http://www.datatrak.net
Aaron Morton
2006-02-24 11:26:37 UTC
Permalink
You should not be doing things like logging when the finalizer is
running. It is entirely possible that the logger your code is trying to
call (or the objects it uses) has been garbage collected by the time
your finalizer is called, or at the very least it has been marked for GC.

This is from the link you sent...
"If /disposing/ equals *false*, the method has been called by the
runtime from inside the finalizer and only unmanaged resources can be
disposed. When an object is executing its finalization code, it should
not reference other objects, because finalizers do not execute in any
particular order. If an executing finalizer references another object
that has already been finalized, the executing finalizer will fail."

So the short answer is don't try to log when called from the finalizer
because the logger is in an unknown state.

aaron
Post by Michael Hinkel
All,
I'd like to log some information in a
protected virtual void Dispose(bool disposing)
method which can be called either by Dispose() or by the finalizer
(implemented according to Microsoft's proposal "Implementing a Dispose
Method"; see
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconimpleme
ntingdisposemethod.htm).
If Dispose(bool disposing) is called by Dispose() everything works fine,
but if the method is calld by the finalizer on application shutdown, no
log entry is created. If I use a Console.WriteLine(message) the message
is sent to the Console even on application shutdown.
Any suggestions?
Michael
_______________________________________
DATATRAK Deutschland GmbH
Michael Hinkel
Software Developer
Rochusstrasse 65
53123 Bonn
http://www.datatrak.net
Nicko Cadell
2006-02-24 12:24:05 UTC
Permalink
For details of what resources can be accessed from a finalizer see
section "3.6 Implementation of Finalize-Time" in the following white
paper:

http://www.gotdotnet.com/team/libraries/whitepapers/resourcemanagement/r
esourcemanagement.aspx

Executive summary: Be very carful avout what you access in your
finalizer and in general only use this to clean up native resources,
e.g. handles that you may have opened.

Nicko
-----Original Message-----
Sent: 24 February 2006 11:27
To: Log4NET User
Subject: Re: logging not possible in finalizer on application
shutdown?
You should not be doing things like logging when the
finalizer is running. It is entirely possible that the logger
your code is trying to call (or the objects it uses) has been
garbage collected by the time your finalizer is called, or at
the very least it has been marked for GC.
This is from the link you sent...
"If disposing equals false, the method has been called by the
runtime from inside the finalizer and only unmanaged
resources can be disposed. When an object is executing its
finalization code, it should not reference other objects,
because finalizers do not execute in any particular order. If
an executing finalizer references another object that has
already been finalized, the executing finalizer will fail."
So the short answer is don't try to log when called from the
finalizer because the logger is in an unknown state.
aaron
All,
I'd like to log some information in a
protected virtual void Dispose(bool disposing)
method which can be called either by Dispose() or by
the finalizer
(implemented according to Microsoft's proposal
"Implementing a Dispose
Method"; see
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cp
conimpleme
ntingdisposemethod.htm).
If Dispose(bool disposing) is called by Dispose()
everything works fine,
but if the method is calld by the finalizer on
application shutdown, no
log entry is created. If I use a
Console.WriteLine(message) the message
is sent to the Console even on application shutdown.
Any suggestions?
Michael
_______________________________________
DATATRAK Deutschland GmbH
Michael Hinkel
Software Developer
Rochusstrasse 65
53123 Bonn
http://www.datatrak.net
Loading...