0

私のウェブページの1つが私に与えています:

GET url_plus_query_string_go_here 500 (Internal Server Error)

エラーの種類。ページ/サーバーを設定して、これらのエラーをログに記録し、それらの詳細とその理由を確認するにはどうすればよいですか?

4

2 に答える 2

0

クラス Log.cs を作成し、 write a メソッドを使用して例外をテキスト ファイルに記録できます。コード内でこのメソッドを呼び出します (例: catch ブロック内)。次のコードを使用して、クラス log.cs を作成します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestApp
{
public class Log
{
    protected static string GetFileName()
    {
        return "log.txt";
    }

    public Log()
    {

    }

    public static void Write(string message)
    {
        System.IO.File.AppendAllText(GetFullFilePath(), message + Environment.NewLine);
    }



    public static string ReadAll()
    {
        return System.IO.File.ReadAllText(GetFullFilePath());
    }

    private static string GetFullFilePath()
    {
        return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GetFileName());
    }
}
}

次のコードを適用して、実行時に追跡する例外またはその他のものを挿入します。ここでは catch ブロックのみを示していますが、コードのどこでも使用できます。

    catch (Exception ex)
        {
          Log.Write(DateTime.Now + "  " + ex.Message + "  Something relevant string");
        }
于 2013-07-02T12:09:51.860 に答える