2008-10-23 78 views

回答

4

我正在爲我們寫的產品鑽研類似的東西。我正在考慮使用PostSharp for Silverlight將客戶端日誌記錄添加爲一個方面。

我已經使用了大獲成功的NLOG項目之前下足.NET Framework和Compact Framework的,所以我將最有可能利用現有的框架代碼,並添加一些記錄目標:

  • 標準體系。診斷目標以啓用使用DebugView等進行捕獲。
  • 異步Web服務目標類似於NLog中的目標。
  • 具有延遲傳輸到服務器語義的隔離存儲目標。

我簡單地看過Clog,它似乎有一個主要缺陷 - 它無法記錄連接失敗。因此,假設您的Web服務器始終處於聯機狀態,可以,但是當上遊或服務器本身發生問題時,日誌記錄數據將全部丟失,甚至可能會導致應用程序崩潰。

+0

`系統。Diagnostics`和*** DebugView ***對於開發環境是好的(並且更好),不適用於生產環境(我無法訪問生產和使用_DebugView_) – Kiquenet 2016-04-29 06:27:52

0

我已經結束了從頭開始寫一個新的日誌框架,解決這個缺陷。我創建了一個本地隊列,它將獲取日誌/跟蹤消息,然後執行過濾並將它們發送到服務器。然後,隊列將由獨立存儲提供支持,因此即使客戶端在該會話中永久離線,消息也會在其恢復聯機時發送。

6

如果您只是想將調試消息輸出到控制檯。您可以使用瀏覽器的console.log機制。我編寫了一個擴展方法。你可以在my blog找到。

// http://kodierer.blogspot.com.es/2009/05/silverlight-logging-extension-method.html 
    public static string Log(string message) 
    { 
     var msgLog = ""; 
     try 
     { 

      HtmlWindow window = HtmlPage.Window; 

      //only log if a console is available 
      var isConsoleAvailable = (bool)window.Eval("typeof(console) != 'undefined' && typeof(console.log) != 'undefined'"); 

      if (!isConsoleAvailable) return "isConsoleAvailable " + isConsoleAvailable; 

      var createLogFunction = (bool)window.Eval("typeof(ssplog) == 'undefined'"); 
      if (createLogFunction) 
      { 
       // Load the logging function into global scope: 
       string logFunction = @"function ssplog(msg) { console.log(msg); }"; 
       string code = string.Format(@"if(window.execScript) {{ window.execScript('{0}'); }} else {{ eval.call(null, '{0}'); }}", logFunction); 
       window.Eval(code); 
      } 

      // Prepare the message 
      DateTime dateTime = DateTime.Now; 
      string output = string.Format("{0} - {1} - {2}", dateTime.ToString("u"), "DEBUG", message); 

      // Invoke the logging function: 
      var logger = window.Eval("ssplog") as ScriptObject; 
      logger.InvokeSelf(output); 
     } 
     catch (Exception ex) 
     { 
      msgLog = "Error Log " + ex.Message; 
     } 
     return msgLog; 

    } 
0

我正在使用JavaScript窗口並在Silverlight中對其進行腳本編寫。對於「生產」,我可以關閉此窗口,但仍將日誌行保存到內存中,如果出現問題,請將其發送到服務器。這樣我就可以得到兩全其美的效果 - 在客戶端上進行簡單,實時的日誌記錄以進行調試,並記錄用戶可能遇到的遠程驗屍情況。

14

如果你願意把你的宇航員頭盔關閉一分鐘,下面是我爲Silverlight編寫的一個輕量級記錄器,用於客戶端記錄(主要用於WCF操作,但可能用於任何錯誤) 。

它最初用於iPhone應用程序的Monotouch,並且已被改編爲IsolateStorage。如果需要,您可以使用Read方法在文本框中顯示。經過SL4測試。

/// <summary> 
/// A lightweight logging class for Silverlight. 
/// </summary> 
public class Log 
{ 
    /// <summary> 
    /// The log file to write to. Defaults to "dd-mm-yyyy.log" e.g. "13-01-2010.log" 
    /// </summary> 
    public static string LogFilename { get; set; } 

    /// <summary> 
    /// Whether to appendthe calling method to the start of the log line. 
    /// </summary> 
    public static bool UseStackFrame { get; set; } 

    static Log() 
    { 
     LogFilename = string.Format("{0}.log", DateTime.Today.ToString("dd-MM-yyyy")); 
     UseStackFrame = false; 
    } 

    /// <summary> 
    /// Reads the entire log file, or returns an empty string if it doesn't exist yet. 
    /// </summary> 
    /// <returns></returns> 
    public static string ReadLog() 
    { 
     string result = ""; 
     IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite(); 

     if (storage.FileExists(LogFilename)) 
     { 
      try 
      { 
       using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.OpenOrCreate,storage)) 
       { 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
         result = reader.ReadToEnd(); 
        } 
       } 
      } 
      catch (IOException) 
      { 
       // Ignore 
      } 
     } 

     return result; 
    } 

    /// <summary> 
    /// Writes information (not errors) to the log file. 
    /// </summary> 
    /// <param name="format">A format string</param> 
    /// <param name="args">Any arguments for the format string.</param> 
    public static void Info(string format, params object[] args) 
    { 
     WriteLine(LoggingLevel.Info, format, args); 
    } 

    /// <summary> 
    /// Writes a warning (non critical error) to the log file 
    /// </summary> 
    /// <param name="format">A format string</param> 
    /// <param name="args">Any arguments for the format string.</param> 
    public static void Warn(string format, params object[] args) 
    { 
     WriteLine(LoggingLevel.Warn, format, args); 
    } 

    /// <summary> 
    /// Writes a critical or fatal error to the log file. 
    /// </summary> 
    /// <param name="format">A format string</param> 
    /// <param name="args">Any arguments for the format string.</param> 
    public static void Fatal(string format, params object[] args) 
    { 
     WriteLine(LoggingLevel.Fatal, format, args); 
    } 

    /// <summary> 
    /// Writes the args to the default logging output using the format provided. 
    /// </summary> 
    public static void WriteLine(LoggingLevel level, string format, params object[] args) 
    { 
     string message = string.Format(format, args); 

     // Optionally show the calling method 
     if (UseStackFrame) 
     { 
      var name = new StackFrame(2, false).GetMethod().Name; 

      string prefix = string.Format("[{0} - {1}] ", level, name); 
      message = string.Format(prefix + format, args); 
     } 

     Debug.WriteLine(message); 
     WriteToFile(message); 
    } 

    /// <summary> 
    /// Writes a line to the current log file. 
    /// </summary> 
    /// <param name="message"></param> 
    private static void WriteToFile(string message) 
    { 
     try 
     { 
      IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite(); 
      bool b = storage.FileExists(LogFilename); 

      using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.Append,storage)) 
      { 
       using (StreamWriter writer = new StreamWriter(stream)) 
       { 
        writer.WriteLine("[{0}] {1}", DateTime.UtcNow.ToString(), message); 
       } 
      } 
     } 
     catch (IOException) 
     { 
      // throw new Catch22Exception(); 
     } 
    } 
} 

/// <summary> 
/// The type of error to log. 
/// </summary> 
public enum LoggingLevel 
{ 
    /// <summary> 
    /// A message containing information only. 
    /// </summary> 
    Info, 
    /// <summary> 
    /// A non-critical warning error message. 
    /// </summary> 
    Warn, 
    /// <summary> 
    /// A fatal error message. 
    /// </summary> 
    Fatal 
} 
+0

`Read`方法在文本框中顯示?使用IsolatedStorageFile的另一種選擇? – Kiquenet 2016-05-03 09:25:24