使用 NLog 记录到隔离存储中

发布于 2024-09-29 03:33:44 字数 142 浏览 0 评论 0原文

我在 C# Win Forms 中使用 NLog Libary (http://nlog-project.org/) 来进行日志记录。 有没有人有一些经验是否可以使用此 NLogger 将日志文件写入“IsolatedStorage”?

谢谢 4 个答案

I use the NLog Libary (http://nlog-project.org/) within my C# Win Forms to do the logging.
Has anyone some experiance if it's possible to write the Logfile into an "IsolatedStorage" with this NLogger?

Thx 4 answers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-10-06 03:33:44

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

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

这是另一个日志记录示例(在 Chris S 的回答中)到隔离存储。

最后,NLog 2.0 确实附带了 LogReceiveService 和 LogReceiverServiceTarget,我认为它们可以从 Silverlight 客户端使用。我还没有这样做,所以我无法评论它们是否有效或如何工作。

按照克里斯蒂安的例子,你可以做这样的事情(我没有尝试过):

[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) 
    { 
    } 
  }
}

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

也许保持流和文件打开会更好只要目标还活着。可以通过重写 InitializeTarget 和 CloseTarget 来做到这一点。

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

一些错误处理可能会有用。至少检测隔离存储是否已耗尽并且可能优雅地(或静默地)失败。

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

I have not used NLog in Silverlight, but a new version, 2.0, has just been released into beta and it is usable in Silverlight (there are some examples on the website). I have not seen an Isolated Storage Target, but I bet it would not be difficult to write one.

This link shows (in Christian's answer) one way to "log" to isolated storage. I can't comment on whether or not it is a good idea. With that information, you could probably write a NLog Target that could be configured into NLog so that NLog loggers could write to isolated storage.

Here is another example (in the answer by Chris S) of logging to isolated storage.

Finally, NLog 2.0 does come with a LogReceiveService and a LogReceiverServiceTarget that I think are usable from a Silverlight client. I have not done so, so I cannot comment on if they work or how they work.

Going by Christian's example, you could do something like this (I have not tried this):

[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) 
    { 
    } 
  }
}

Some things I can think of that might improve this are:

Maybe it is better to keep the stream and the file open as long as the Target is alive. Could probably do this by overriding InitializeTarget and CloseTarget.

Maybe it would be nice to allow the filename to be specified rather than using hardcoded filename.

Some error handling might be useful. At least detecting if the isolated storage has been exhausted and maybe failing gracefully (or silently).

If you go this route, Good Luck! Report back on whether you are successful or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文