2016-09-09 7 views
0

ログファイルに.txtのように重要なものを記録したいだけです。 私はこのためにErrorHandlingクラスを作成しました。私はCSVファイルからエラーコードを読み、コンソール/ GUIにテキストを追加します。今は問題です、私は別のUserControlsからエラーファイルに書き込みたいが、コンソールはMainWindowにあります。私はどのように私のクラスから出力コンソールにテキストを追加できますか?異なるUserControlからログファイルを書き込む最良の方法

これは私のErrorHandlingクラスです。

public class ErrorHandling 
    { 
    public static Action WriteErrorLog; 
    private static object writeLock = new object(); 

    public enum errorState 
    { 
     INFO, WARNING, CRIT 
    } 

    public string getErrorString(int ErrorCode) 
    { 
     var errorString = string.Empty; 
     var reader = new StreamReader(File.OpenRead(@"errorCodes.csv")); 

     List<string> Codes = new List<string>(); 
     List<string> Error = new List<string>(); 

     while (!reader.EndOfStream) 
     { 
      var line = reader.ReadLine(); 
      var values = line.Split(';'); 

      Codes.Add(values[0]); 
      Error.Add(values[3]); 
     } 

     var index = Codes.FindIndex(p => p == ErrorCode.ToString()); 

     if (index == -1) 
      return "Unbekannter Fehler aufgetreten!"; 
     else 
      return Error[index] + " (" + Codes[index] + ")"; 
    } 

    public void addToLogFile(errorState error, int errorCode = 0, string errorText = null) 
    { 
     var errorTextResult = error.ToString(); 

     if (errorCode == 0 && errorText == null) 
     { 
      return; 
     } 
     else if (errorCode != 0 && errorText != null) 
     { 

      errorTextResult += " - " + DateTime.Now.ToString(); 
      errorTextResult += " : " + getErrorString(errorCode) + "\n"; 
      errorTextResult += errorText; 

     } 
     else if (errorCode != 0 && errorText == null) 
     { 
      errorTextResult += " - " + DateTime.Now.ToString(); 
      errorTextResult += " : " + getErrorString(errorCode); 
     } 
     else if (errorCode == 0 && errorText != null) 
     { 
      errorTextResult += " - " + DateTime.Now.ToString() + " : "; 
      errorTextResult += errorText; 
     } 

     ThreadPool.QueueUserWorkItem(writeToLogFile, errorTextResult); 

    } 

    #region LogDatei schreiben 
    public static void writeToLogFile(object text) 
    { 
     try 
     { 
      if (!File.Exists(Properties.Settings.Default.LogFilePath)) 
      { 

       using(var writer = File.AppendText(Properties.Settings.Default.LogFilePath)) 
        writer.WriteLine(Properties.Settings.Default.LogFilePath, "LatikeManager Logfile " + DateTime.Now.ToString()); 
      } 
      using (var writer = File.AppendText(Properties.Settings.Default.LogFilePath)) 
       writer.WriteLine(Properties.Settings.Default.LogFilePath, text + "\n"); 

      WriteErrorLog(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Fehler beim schreiben der Log Datei : \n" + text + "\n" + ex.ToString()); 
     } 
    } 
    #endregion 
} 

私は別のUserControlからテキストを追加するだけで、そのアクションはErrorHandlingの新しいインスタンスであるため起動しません。

+2

既存のログフレームワークを採用してください。なぜあなたはそれをやり直し、それを全面的に再考する必要がありますか?独自のソリューションをデバッグするのにかかる時間は、それを採用するよりもはるかに時間がかかります。 –

答えて

0

実用的な解像度が見つかりました。

私は現在、トレース方法を使用しています。

はちょうど私のApp.conf

<system.diagnostics> 
    <trace autoflush="true"> 
     <listeners> 
     <add name="myTrace" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\LogFile\mytrace.log"></add> 
     </listeners> 
    </trace> 
    </system.diagnostics> 

にこのコードを追加しました。そして、私のErrorHandling方法で

public static void writeToLogFile(object text) 
{ 
    Trace.Write(string.Format("{0}\r\n",text)); 
} 

このコードを使用していました。これは私のためにうまくいった。

関連する問題