开发者

Server crash due to ASP.NET-Page (C#)

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(Not开发者_运维百科e, 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...


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.


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


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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜