在SDK中使用log4net [英] Use log4net in SDK

查看:193
本文介绍了在SDK中使用log4net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#提供一个SDK。要启用现场调试,我想使用log4net包括日志记录。如何启用配置而不使用App.config,因为程序集将是一个dll?



谢谢,

解决方案

考虑使用XmlConfigurator为log4net配置独立的配置文件位置。您可以使用此技术提供独立的基于文件的配置,而无需触摸app \web.config,或者必须对其进行硬编码。示例:



http: //haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx



更新



尝试以下引导程序来配置独立配置文件。它将构建一个完整的路径。尝试注销路径,如果仍然发现问题仍然存在问题。

  public static class LogFactory 
{
public const string ConfigFileName =log4net.config;

public static void Configure()
{
Type type = typeof(LogFactory);
FileInfo assemblyDirectory = AssemblyInfo.GetCodeBaseDirectory(type);
string path = Path.Combine(assemblyDirectory.FullName,ConfigFileName);
FileInfo configFile = new FileInfo(path);
XmlConfigurator.ConfigureAndWatch(configFile);
log4net.ILog log = LogManager.GetLogger(type);
log.ToString();
}
}

致电:

  LogFactory.Configure(); 


I am providing an SDK using C#. To enable field debugging, I want to include logging using log4net. How to enable configuration without using App.config since the assembly will be a dll?

Thanks,

解决方案

Consider using XmlConfigurator to configure a standalone config file location for log4net. You can use this technique to supply independent file-based configuration without having to touch app\web.config, or having to hard-code it. Example:

http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

Update

Try the following bootstrapper to configure the standalone config file. It will construct a full path. Try logging out the path if it still appears to be having problems finding it.

public static class LogFactory
{
    public const string ConfigFileName = "log4net.config";

    public static void Configure()
    {
        Type type = typeof(LogFactory);
        FileInfo assemblyDirectory = AssemblyInfo.GetCodeBaseDirectory(type);
        string path = Path.Combine(assemblyDirectory.FullName, ConfigFileName);
        FileInfo configFile = new FileInfo(path);
        XmlConfigurator.ConfigureAndWatch(configFile);
        log4net.ILog log = LogManager.GetLogger(type);
        log.ToString();
    }
}

Call:

LogFactory.Configure();

这篇关于在SDK中使用log4net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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