是被动的记录有可能在.NET? [英] Is passive logging possible in .NET?

查看:107
本文介绍了是被动的记录有可能在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对采伐我在code,包括量屡屡受挫,这让我不知道是否有做事情的一个更好的办法。

I'm frequently frustrated by the amount of logging I have to include in my code and it leads me to wonder if there's a better way of doing things.

我不知道这是否已经做了,或者如果有人想出了一个更好的想法,但我想知道是否有办法的人都知道的注入的记录程序到应用程序,使其被动监视线程静静地记录所发生的过程,而无需做这样的事情:

I don't know if this has been done or if someone has come up with a better idea but I was wondering is there a way anyone knows of to "inject" a logger into an application such that it passively monitors the thread and quietly logs processes as they occur without having to do things like:

public void MyProcess(int a, string b, object c)
{
  log(
    String.Format(
      "Entering process MyProcess with arguments: [a] = [{0}]; [b] = [{1}]; [c] = [{2}]",
      a.ToString(),
      b,
      c.ToString()
  );

  try
  {
    int d = DoStuff(a)
    log(
      String.Format(
        "DoStuff({0}) returned value {1}",
        a.ToString(),
        d.ToString()
      )
    );
  }
  catch (Exception ex)
  {
    log(
      String.Format("An exception occurred during process DoStuff({0})\nException:\n{1}",
      a.ToString(),
      ex.ToString())
    )
  }
}

什么是伟大的是,如果我可以说,我的记录:

What would be great is if I could say to my logger:

Monitor(MyClass.MyMethod)

然后它会监视一切的推移包括传入的参数,并与方法调用,并传递给这些方法的值的方法内,发生等异常。

It would then monitor everything that goes on inside of that method including passed in arguments along with method calls and values passed into those methods, exceptions that occur etc.

有没有人实现这样的事情在过去的?难道连的的实施?以这种方式正在记录只是一个白日梦?

Has anyone implemented something like this in the past? Could it even be implemented? Is logging in this fashion just a pipe dream?

我喜欢设计东西会做到这一点,但我甚至不知道我会开始。当然,我不希望推倒重来或者,如果它已经完成,这将是巨大的,如果有人可以点我在正确的方向。

I'd love to design something that would do this, but I just don't even know where I'd begin. Of course, I don't want to reinvent the wheel either, if it's already been done, it would be great if someone could point me in the right direction.

任何建议将受到欢迎...

Any suggestions would be gratefully received...

编辑:我想我会在其查询以详细的日志中所要求的水平答复意见。这是经常需要记录的配置水平可以提供这样的,如果配置指定详细的日志记录,然后一切都记录下来,而如果关键的日志记录配置则只有某些信息以及异常记录。如果致命的日志记录配置这会导致应用程序死掉将被记录,然后仅供参考。请问这样的事情是配置还是会AOP需要3或4个不同的建立取决于日志记录级别是多少?

I thought I'd comment on an answer which queried as to the level of detail required in the log. It is frequently required that configurable levels of logging be provided such that if the configuration specifies detailed logging then everything is logged, whereas if critical logging is configured then only certain information is logged along with exceptions. If fatal logging is configured then only information which causes the application to die would be logged. Would something like this be configurable or would AOP require 3 or 4 different builds depending on the number of logging levels?

我经常使用的4个级别:致命,关键的信息,详细

I frequently use 4 levels: Fatal, Critical, Information, Detailed

推荐答案

您可以使用 PostSharp 以绕的方式登录。这也正是那种东西AOP擅长。你可能要开始与 Log4PostSharp - 插件专门用于记录

You can use PostSharp to log "around" the method. This is exactly the kind of thing AOP is good at. You might want to start with Log4PostSharp - a plugin specifically for logging.

这篇关于是被动的记录有可能在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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