Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
.net 使用NLog将日志记录到隔离存储中_.net_Winforms_Isolatedstorage_Nlog - Fatal编程技术网

.net 使用NLog将日志记录到隔离存储中

.net 使用NLog将日志记录到隔离存储中,.net,winforms,isolatedstorage,nlog,.net,Winforms,Isolatedstorage,Nlog,我使用NLog图书馆(http://nlog-project.org/)在我的C#Win表单中进行日志记录。 如果可以用这个NLogger将日志文件写入“隔离存储”中,有人有经验吗 Thx 4 answers我没有在Silverlight中使用NLog,但新版本2.0刚刚发布到beta版,可以在Silverlight中使用(网站上有一些示例)。我还没有看到一个孤立的存储目标,但我打赌编写一个目标并不困难 (在Christian的回答中)一种“记录”到隔离存储的方法。我不能评论这是否是个好主意。有

我使用NLog图书馆(http://nlog-project.org/)在我的C#Win表单中进行日志记录。 如果可以用这个NLogger将日志文件写入“隔离存储”中,有人有经验吗


Thx 4 answers

我没有在Silverlight中使用NLog,但新版本2.0刚刚发布到beta版,可以在Silverlight中使用(网站上有一些示例)。我还没有看到一个孤立的存储目标,但我打赌编写一个目标并不困难

(在Christian的回答中)一种“记录”到隔离存储的方法。我不能评论这是否是个好主意。有了这些信息,您可能可以编写一个NLog目标,该目标可以配置为NLog,以便NLog记录器可以写入到隔离存储中

(在Chris S的回答中)记录到隔离存储

最后,NLOG2.0提供了一个LogReceiveService和一个LogReceiveServiceTarget,我认为可以从Silverlight客户端使用它。我还没有这样做,所以我无法评论它们是否有效或如何有效

以Christian为例,你可以这样做(我没有尝试过):

我能想到的一些可能改善这一点的事情是:

也许只要目标还活着,就最好保持流和文件处于打开状态。可以通过重写InitializeTarget和CloseTarget来实现这一点

也许允许指定文件名而不是使用硬编码文件名会更好

一些错误处理可能很有用。至少要检测隔离存储是否已耗尽,并且可能会正常(或无声)失败

如果你走这条路,祝你好运!汇报你是否成功

[Target("IsolatedStorage")]
public sealed class IsolatedStorageTarget : TargetWithLayout    
{        
  public IsolatedStorageTarget()
  {            
  }
  protected override void Write(LogEventInfo logEvent)        
  {
    try 
    { 
      using (IsolatedStorageFile store = 
             IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
        using (Stream stream = new IsolatedStorageFileStream
               ("Solution.Silverlight.log", FileMode.Append, FileAccess.Write, store)) 
        { 
          StreamWriter writer = new StreamWriter(stream); 
          writer.WriteLine(this.Layout.Render(logEvent));
        } 
        writer.Close(); 
      } 
    } 
    catch (Exception ex) 
    { 
    } 
  }
}