调试后台服务上的堆栈溢出错误

han*_*odb 10 c# debugging windows-services logfile

这不是一个真正的问题,而是一个有希望帮助其他人的答案.

那些以前写过Windows服务的人,知道在其中发现错误的任务是什么,特别是如果它只发生在实时环境中.就我而言,我有一个服务运行了几个小时,然后从堆栈溢出错误中解决了.没有堆栈跟踪.祝你在大海捞针中找到针.

该服务确实生成了一个日志文件,代码中充斥着日志条目,但详细说明,它生成了500 MB的日志文件!你几乎无法打开文件,不要介意分析它.但是你如何解决这个问题呢?您可以尝试生成信息较少的日志文件,或者在编写较新的日志条目时自动删除旧日志条目,但随后会丢失重要的错误上下文.

解决方案是一个日志文件,它将跟踪代码中的循环,并自动删除该循环的每次成功迭代的日志条目.这样,您可以维护一个高度拘留的日志文件,该文件同时保持相对较小.当您的服务中断时,您的日志文件将告诉您确切的位置,以及解释其发生方式和原因的所有必要上下文.

您可以从http://sourceforge.net/projects/smartl/files/?source=navbar下载此日志文件生成器.它是一个独立的类,它的所有方法都是静态的.提供了一个示例类,向您展示如何正确使用日志记录方法:

    public void ExampleMethod()
    {           
        SmartLog.EnterMethod("ExampleMethod()"); 
        try
        {
            SmartLog.Write("Some code happening before the loop");

            Guid exampleLoopID = SmartLog.RegisterLoop("exampleLoopID");
            for (int i = 0; i < 10; i++)
            {
                SmartLog.IncrementLoop(exampleLoopID);

                SmartLog.Write("Some code happening inside the loop.");

            }
            SmartLog.CompleteLoop(exampleLoopID);

            SmartLog.Write("Some code happening after the loop.");

            SmartLog.LeaveMethod("ExampleMethod()");
        }
        catch (Exception ex)
        {
            SmartLog.WriteException(ex);
            SmartLog.LeaveMethod("ExampleMethod()");
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

确保您的应用程序在其根文件夹上具有读写访问权限.

如果您执行代码,并在循环内部将其分解,则日志文件将如下所示:

. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:

. . ENTER METHOD: ExampleMethod()
some code happening before the loop.

LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - CURRENT ITERATION: 20
some code happening inside the loop.
Run Code Online (Sandbox Code Playgroud)

循环完成后,其内容将被删除,您的日志文件将如下所示:

. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:

. . ENTER METHOD: ExampleMethod()
some code happening before the loop.

LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - TOTAL ITERATIONS: 22

some code happening after the loop.
. . LEAVING METHOD: ExampleMethod()

some code happening here.
some code happening here.
. LEAVING METHOD: FirstMethod()
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助某人解决这个问题,否则可能需要数周的时间.这肯定对我有用.

Dav*_*kes 0

解决方案是使用一个日志文件来跟踪代码中的循环,并自动删除该循环每次成功迭代的日志条目。这样,您可以维护一个高度保留的日志文件,同时该文件仍然相对较小。当您的服务中断时,您的日志文件会准确地告诉您发生的情况,以及解释发生的方式和原因的所有必要上下文。

您可以从http://sourceforge.net/projects/smartl/files/?source=navbar下载此日志文件生成器。它是一个独立的类,它的所有方法都是静态的。提供了一个示例类来向您展示如何正确使用日志记录方法:

public void ExampleMethod()
{           
    SmartLog.EnterMethod("ExampleMethod()"); 
    try
    {
        SmartLog.Write("Some code happening before the loop");

        Guid exampleLoopID = SmartLog.RegisterLoop("exampleLoopID");
        for (int i = 0; i < 10; i++)
        {
            SmartLog.IncrementLoop(exampleLoopID);

            SmartLog.Write("Some code happening inside the loop.");

        }
        SmartLog.CompleteLoop(exampleLoopID);

        SmartLog.Write("Some code happening after the loop.");

        SmartLog.LeaveMethod("ExampleMethod()");
    }
    catch (Exception ex)
    {
        SmartLog.WriteException(ex);
        SmartLog.LeaveMethod("ExampleMethod()");
        throw;
    }
}
Run Code Online (Sandbox Code Playgroud)

确保您的应用程序对其根文件夹具有读写访问权限。

如果执行代码并在循环内中断它,日志文件将如下所示:

. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:

. . ENTER METHOD: ExampleMethod()
some code happening before the loop.

LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - CURRENT ITERATION: 20
some code happening inside the loop.
Run Code Online (Sandbox Code Playgroud)

循环完成后,其内容将被删除,您的日志文件将如下所示:

. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:

. . ENTER METHOD: ExampleMethod()
some code happening before the loop.

LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - TOTAL ITERATIONS: 22

some code happening after the loop.
. . LEAVING METHOD: ExampleMethod()

some code happening here.
some code happening here.
. LEAVING METHOD: FirstMethod()
Run Code Online (Sandbox Code Playgroud)