0

スクリプトのバックグラウンドから来て、私はc#と.netで働き始めました。プロセスが完了したことを通知するテキストファイルを作成するクラスを作成しました。このクラスをコンソールアプリケーションでインスタンス化し、最後に書き込みプロセスを呼び出します。私がやろうとしているのは、エラーや警告が発生したときに他のすべてのクラスを追跡し、そのイベントを通知に反映することです。

これをpublicstaticboolsで実装したので、実行中のクラスに関係なく、catchが発生したときに、boolをtrueに更新できます。これは間違っていると感じますが、現時点では自分が何をすべきかを見つけるのに十分な知識がありません。

誰か助けてもらえますか?コード例:

namespace Logging
{
public class Notice
{
    public static bool HasWarning { get; set; }
    public static bool HasError { get; set; }

    public string ProcessName {get; set;}


    public enum Status
    {
        [Description("Complete Success")]
        Complete_Success,
        [Description("Complete with Warnings")]
        Complete_With_Warnings,
        [Description("Error Incomplete")]
        Error_Incomplete
    }


    public void EndProcess(Status status)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(string.Format("{0} is Complete!", ProcessName));
        sb.Append(Environment.NewLine);
        sb.Append(Environment.NewLine);
        sb.Append(string.Format("Completion Status: {0}", ToStringEnums(status)));
        sb.Append(Environment.NewLine);
        sb.Append(string.Format("Completion Time: {0}", DateTime.Now.ToString("MM/dd/yy H:mm:ss")));

        File.WriteAllText(string.Format(@"{0}\EndProcess.txt", Directory.GetCurrentDirectory()), sb.ToString());
    }

これは私がそれを呼ぶところです:

        private static void WriteNotice()
    {
        Notice newNotice = new Notice();
        newNotice.ProcessName = "NCOA";
        if (Notice.HasError == true)
            newNotice.EndProcess(Notice.Status.Error_Incomplete);
        else if (Notice.HasWarning == true)
            newNotice.EndProcess(Notice.Status.Complete_With_Warnings);
        else
            newNotice.EndProcess(Notice.Status.Complete_Success);

    }
4

1 に答える 1

0

コードの周りにTryCatchステートメントを使用して、次のようにトレースすることをお勧めします。

    try
        {
            //your code goes here

        }
        catch (Exception ex)
        {
            //write exception detail to output window using .Net Tracing Tools
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

より高度なロギングメカニズムについては、次のようにNLOGを使用することをお勧めします。

    static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    try
    {
    //your code
    }
    catch (Exception ex)
    {
    logger.Error(ex.Message + System.Reflection.MethodBase.GetCurrentMethod());
    }
于 2012-12-18T19:11:53.450 に答える