与NLOG登录到一个独立存储 [英] Logging with NLog into an Isolated Storage

查看:226
本文介绍了与NLOG登录到一个独立存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是NLOG Libary(http://nlog-project.org/)在我的C#赢形式做记录。 有没有人一定的experiance是否有可能写入日志文件到IsolatedStorage这个NLogger?

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答案

推荐答案

我没有用NLOG在Silverlight,而是一个新的版本,2.0,刚刚被释放进入测试阶段,它可用在Silverlight(上有一些例子该网站)。我还没有看到一个孤立的存储目标,但我敢打赌,它不会是很难写的。

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.

此链接显示(基督教的回答),以日志,以一种方式独立存储。我不能对是否是一个好主意发表评论。有了这些信息,你很可能编写可以被配置到NLOG一个NLOG目标,这样NLOG记录器可以写入独立存储。

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.

<一个href="http://stackoverflow.com/questions/228723/silverlight-logging-framework-and-or-best-practices">Here是登录到独立存储的另一个例子(由克里斯·S中的答案)。

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

最后,NLOG 2.0的确有LogReceiveService和LogReceiverServiceTarget,我认为是从Silverlight客户端使用。我没有这样做,所以我不能发表评论,如果他们的工作或者他们是如何工作的。

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:

也许最好是保持流和文件,只要目标是活着打开。很可能通过重写InitializeTarget和CloseTarget做到这一点。

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.

也许这将是很好,允许指定的文件名,而不是使用硬codeD文件名。

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.

这篇关于与NLOG登录到一个独立存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆