views:

136

answers:

5

My local test server crashes as soon as I'm trying to write to a logfile. I'm using this for an ASP.NET-Page, codebehind is C#.

Structure:

/
 Functions.cs
 index.aspx
 index.aspx.cs

I create an instance of Functions as my index.aspx loads. In Functions, I define a function for logging, which is called from index.aspx.cs and looks like this:

if (_WriterOpen == false)
{
    _Writer = new StreamWriter(_WorkingDir + _Logfile, true);
    _WriterOpen = true;
    _Writer.AutoFlush = true;
}
_Writer.WriteLine(DateTime.Now.ToString() + ": " + String.Format(Note, Args));

_Writer is defined globally for the Class and contains, as you see, a StreamWriter. The class itself has a destructor to close any connections to the files;

~Functions()
{
    _Writer.Flush();
    _Writer.Close();
    _Writer.Dispose();
}

So, when I open up my page, the logs are written but then the server crashes. So I assume the problem is somewhere in the descructor, but I can't figure out why...

+1  A: 

This sounds most likely to be a stack overflow error, caused by a recursive call.

Are you logging an error, that is causing an error, that is logging, causing...etc.

Can you run it in debug mode from Visual Studio?

ck
Stack overflow is not given, as i don't log errors at all, but important messages sent during the calculation the page does. It's so to say not an error log but a "look what I did"-log.
ApoY2k
+3  A: 

You don't need any destructor, StreamWriter already have it's own.

You should not access other objects from a destructor as it will be called by the Garbage collector, the other objects are in an undeterminable state. You don't know when it will be called and you don't know on which thread it will be called. NEVER, NEVER, NEVER write a destructor, it's almost always a bad idea.

You may place your cleanup code in the Unload event, see ASP.NET Page Life Cycle.

Guillaume
Oh that's new... okay, I did that because I sometimes got an error stating I couldn't write to the file because it was `being used by another process` so I decided to close it manually.
ApoY2k
Okay, so I'm not using it anymore and clode the Writer after I wrote a note into it. Works fine and performance should be okay.
ApoY2k
+1  A: 

First: you don't need to call both close and dispose. See the link from MSDN: link text

This implementation of Close calls the Dispose method passing a true value.

You must call Close to ensure that all data is correctly written out to the underlying stream. Following a call to Close, any operations on the StreamWriter might raise exceptions. If there is insufficient space on the disk, calling Close will raise an exception.

24x7Programmer
Okay, thanks. Just wanted to make sure ;-)
ApoY2k
A: 

If you cant run it through in the debugger, try commenting lines out of Functions until it stops falling over. This should give you a clue.

The garbage collector will call the destructor at some indeterminate time, so if you are getting a regular crash, its probably not the destructor.

I would prefer to make the class inherit from IDisposable and put the clean up code in there. Use the dispose pattern : here

Mongus Pong
+1  A: 

I'd recommend the using statement which automatically invokes the dispose call. For your purposes the code would look something like:

StreamWriter _Writer;

using(_Writer)
{
    if (_WriterOpen == false)
    {
       _Writer = new StreamWriter(_WorkingDir + _Logfile, true);
       _WriterOpen = true;
       _Writer.AutoFlush = true;
    }
    _Writer.WriteLine(DateTime.Now.ToString() + ": " + String.Format(Note, Args));
}

Note I haven't tested this out but it should work (or at least be close). I'll update if needed after checking it out

Also see the following two articles:
Article 1
Article 2

Jon P
This is a good idea. Thing is, just by now I arrived at the "using"-Statement-chapter of my Visual-C#-Book *gg*
ApoY2k