简体   繁体   English

log4net如何获取Log错误的输出?

[英]log4net how do I get the output of Log errors?

I have ac# web application and I would like to be able to see errors being thrown in the Service.asmx.cs section. 我有一个ac#Web应用程序,我希望能够看到Service.asmx.cs部分中引发的错误。 Ie when the sql statement fails or I spelled a variable name wrong.. etc. I'm fairly new to .net but the template for the app that I have has log4net installed. 即,当sql语句失败或我将变量名拼写错误..等等。我对.net还是很陌生,但是我安装的应用程序模板已安装log4net。

 protected static log4net.ILog Log = EarthSoft.Common.log4net.GetLogger(typeof (Service));

Then my get method is : 然后我的get方法是:

   [WebMethod(Description = "Get a list of facilities")]

public List<Facility> GetFacilities() 
{
  try
  {
    var context = EarthSoft.Server.Helpers.RequestContext.GetRequestContext(true);

    if (context.Connection == null || context.User == null) return null;

    var lst = new List<Facility>();

    string sql =

       string.Format("select analyte_full,conc from dt_biological");

    var cmd = context.Connection.CreateCommand(sql);
    context.Connection.PrepareTextCommand(cmd);
    using (var reader = cmd.ExecuteReader())
    {
      while (reader.Read())
      {
        lst.Add(new Facility()
          {

            name = (string)reader.GetValue(0),
            distance  = (decimal)reader.GetValue(1)
        });
      }
    }
    context.Connection.Close();

    return lst;
  }
  catch (Exception ex)
  {
    Log.Error(ex.Message, ex);
    return null;
  }
}

So the error message is appended to the Log here but I have no idea how to access it or where it writes the log? 因此,错误消息会附加到此处的日志中,但是我不知道如何访问它或将日志写在何处? Is it possible the log output is saved onto a table into the sql database somewhere? 日志输出是否有可能保存到表中的sql数据库中?

Log4Net is higly configurable, and to specify "where" to log, it uses the "appender" concepts. Log4Net是可配置的,并且使用“ appender”概念指定要记录的“位置”。 There is a lot of appender already done that write in files/database/queues and you can write your own, if needed ( normally not ). 已经有很多附加程序可以在文件/数据库/队列中写入,如果需要,您可以编写自己的附加程序(通常不是)。 Anyway start to look at the examples from here: https://logging.apache.org/log4net/release/config-examples.html Somewher ein your code, typically in the application initialization, you have to call: 无论如何,从这里开始查看示例: https : //logging.apache.org/log4net/release/config-examples.html在您的代码中,通常是在应用程序初始化中,您必须调用:

XmlConfigurator.Configure()

This will read the configuration from the application configuration file. 这将从应用程序配置文件中读取配置。 Alternatively you can configure log4net programmatically, but it can be sort of advanced if you never used before with manual configuration, so I suggest to start looking at the examples above and use configuration from file. 另外,您可以通过编程方式配置log4net,但是如果您以前从未使用过手动配置,它可以是高级的,因此,我建议开始查看上述示例并使用文件中的配置。 If no configuration is supplied, log4net work perfectly but... no log are produced at all, and I think this is the reason you can't find any log at the moment. 如果未提供任何配置,则log4net可以正常运行,但是...根本不会生成任何日志,我认为这就是您目前无法找到任何日志的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM