Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Logging Silverlight日志框架和/或最佳实践_Logging_Silverlight 2.0 - Fatal编程技术网

Logging Silverlight日志框架和/或最佳实践

Logging Silverlight日志框架和/或最佳实践,logging,silverlight-2.0,Logging,Silverlight 2.0,现在Silverlight 2终于发布了。我想知道是否有人已经为它准备了日志框架,也许像或?我感兴趣的是可以执行客户端跟踪并将消息记录到服务器的东西 到目前为止,我发现的唯一一个项目是在。有人用过这个吗?您对此有何想法?我将为我们编写的一款产品深入研究类似的内容。我正在考虑使用PostSharp for Silverlight添加客户端日志作为一个方面 我以前在完整的.NET框架和Compact框架下使用过非常成功的NLog项目,因此我很可能会使用现有的框架代码并添加一些日志目标: 一个标准系

现在Silverlight 2终于发布了。我想知道是否有人已经为它准备了日志框架,也许像或?我感兴趣的是可以执行客户端跟踪并将消息记录到服务器的东西


到目前为止,我发现的唯一一个项目是在。有人用过这个吗?您对此有何想法?

我将为我们编写的一款产品深入研究类似的内容。我正在考虑使用PostSharp for Silverlight添加客户端日志作为一个方面

我以前在完整的.NET框架和Compact框架下使用过非常成功的NLog项目,因此我很可能会使用现有的框架代码并添加一些日志目标:

  • 一个标准系统。诊断目标可以使用DebugView等进行捕获
  • 与NLog中类似的异步Web服务目标
  • 具有延迟传输到服务器语义的隔离存储目标

我简要介绍了Clog,它似乎有一个主要缺陷——它无法记录连接故障。因此,假设您的Web服务器一直处于联机状态,是的,它会工作,但当上游或服务器本身出现问题时,日志数据将全部丢失,甚至可能使您的应用程序崩溃。

我最终从头开始编写了一个新的日志框架来解决此缺陷。我创建了一个本地队列,该队列将获取日志/跟踪消息,然后进行筛选并将它们发送到服务器。然后,队列将由独立存储进行备份,因此即使客户端在该会话中永久脱机,消息也将在其重新联机时发送。

如果您只想将调试消息输出到控制台。您可以使用浏览器的console.log机制。我为此编写了一个扩展方法。你可以在上面找到


我正在使用JavaScript窗口,并使其可以在Silverlight中编写脚本。对于“生产”,我可以关闭此窗口,但仍将日志行保存到内存中,然后如果出现问题,将其发送到服务器。这样,我可以两全其美——在客户端进行简单的实时日志调试,并记录用户可能遇到的远程验尸情况。

如果您愿意摘下宇航员的头盔一分钟,下面是我为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
}
//
///Silverlight的轻量级日志记录类。
/// 
公共类日志
{
/// 
///要写入的日志文件。默认为“dd-mm-yyyy.log”,例如“13-01-2010.log”
/// 
公共静态字符串LogFilename{get;set;}
/// 
///是否将调用方法追加到日志行的开头。
/// 
公共静态bool UseStackFrame{get;set;}
静态日志()
{
LogFilename=string.Format(“{0}.log”,DateTime.Today.ToString(“dd-MM-yyyy”);
UseStackFrame=false;
}
/// 
///读取整个日志文件,如果不存在,则返回空字符串。
/// 
/// 
公共静态字符串ReadLog()
{
字符串结果=”;
IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForSite();
if(storage.FileExists(LogFilename))
{
尝试
{
使用(IsolatedStorageFileStream=新的IsolatedStorageFileStream(LogFilename,FileMode.OpenOrCreate,storage))
{
使用(StreamReader=新StreamReader(stream))
{
结果=reader.ReadToEnd();
}
}
}
捕获(IOException)
{
//忽略
}
}
返回结果;
}
/// 
///将信息(而不是错误)写入日志文件。
/// 
///格式字符串
///格式字符串的任何参数。
公共静态无效信息(字符串格式,参数对象[]args)
{
WriteLine(LoggingLevel.Info、格式、参数);
}
/// 
///将警告(非严重错误)写入日志文件
/// 
///格式字符串
///格式字符串的任何参数。
公共静态无效警告(字符串格式,参数对象[]args)
{
WriteLine(LoggingLevel.Warn,格式,参数);
}
/// 
///将严重或致命错误写入日志文件。
/// 
///格式字符串
///格式字符串的任何参数。
公共静态无效致命(字符串格式,参数对象[]args)
{
WriteLine(LoggingLevel.Fatal,格式,参数);
}
/// 
///使用提供的格式将参数写入默认日志记录输出。
/// 
公共静态void WriteLine(日志级别、字符串格式、参数对象[]args)
{
字符串消息=string.Format(格式,args);
//(可选)显示调用方法
如果(使用堆栈帧)
{
var name=new StackFrame(2,false).GetMethod().name;
字符串前缀=string.Format(“[{0}-{1}]”,级别,名称);
message=string.Format(前缀+格式,args);
}
Debug.WriteLine(消息);
写入文件(消息);
}
/// 
///将一行写入当前日志文件。
/// 
/// 
私有静态void WriteToFile(字符串消息)
{
尝试
{
IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForSite();
bool b=storage.FileExists(LogFilename);
使用(IsolatedStorageFileStream=新的IsolatedStorageFileStream(LogFilename,FileMode.Append,storage))
{
使用(StreamWriter=新StreamWriter(流))
/// <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
}