log4net如何获得日志错误的输出



我有一个C#Web应用程序,我希望能够在Service.ASMX.CS部分中看到错误。IE。当SQL语句失败或我拼写一个变量名称错误。等等。等等。

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

然后我的获取方法是:

   [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;
  }
}

因此,错误消息在这里附加到日志,但我不知道如何访问它还是在何处写日志?是否有可能将日志输出保存到某个地方的SQL数据库中?

log4net是可配置的,并且要指定" log log",它使用" appender"概念。已经在文件/数据库/队列中写了很多appender,您可以根据需要(通常不是(编写自己的编写。无论如何,从这里开始查看示例:https://logging.apache.org/log4net/release/config-examples.html通常在应用程序初始化中,您必须致电:

XmlConfigurator.Configure()

这将从应用程序配置文件中读取配置。另外,您可以通过编程方式配置Log4NET,但是如果您从未使用手动配置使用,则可以是高级的,因此我建议开始查看上面的示例并使用文件中的配置。如果没有提供配置,Log4net可以完美地工作,但是...根本没有生成日志,我认为这是您目前找不到任何日志的原因。

最新更新