以编程方式为AdoNetAppender配置log4net

时间:2014-12-12 16:59:57

标签: asp.net-mvc asp.net-mvc-5 log4net

我有以下代码:

public class Log4NetSetup 
{
    public static void Setup()
    {

        #region log4net parameters

        AdoNetAppenderParameter logDate = new AdoNetAppenderParameter()
        {
            ParameterName = "@log_date",
            DbType = System.Data.DbType.DateTime,
            Layout = new RawTimeStampLayout()
        };

        AdoNetAppenderParameter thread = new AdoNetAppenderParameter()
        {
            ParameterName = "@thread",
            DbType = System.Data.DbType.String,
            Size = 255,
            Layout = (IRawLayout)new PatternLayout("%thread")
        };

        AdoNetAppenderParameter logLevel = new AdoNetAppenderParameter()
        {
            ParameterName = "@log_level",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = (IRawLayout)new PatternLayout("%level")
        };

        AdoNetAppenderParameter logger = new AdoNetAppenderParameter()
        {
            ParameterName = "@logger",
            DbType = System.Data.DbType.String,
            Size = 255,
            Layout = (IRawLayout)new PatternLayout("%logger")
        };

        AdoNetAppenderParameter message = new AdoNetAppenderParameter()
        {
            ParameterName = "@message",
            DbType = System.Data.DbType.String,
            Size = 4000,
            Layout = (IRawLayout)new PatternLayout("%message")
        };

        AdoNetAppenderParameter exception = new AdoNetAppenderParameter()
        {
            ParameterName = "@exception",
            DbType = System.Data.DbType.String,
            Size = 2000,
            Layout = (IRawLayout)new ExceptionLayout()
        };

        #endregion


        AdoNetAppender dotNet = new AdoNetAppender()
        {
            BufferSize = 1,
            CommandType = System.Data.CommandType.Text,
            ConnectionType = "REDACTED",
            ConnectionString = "REDACTED",
            CommandText = "REDACTED",
        };


        dotNet.AddParameter(logDate);
        dotNet.AddParameter(thread);
        dotNet.AddParameter(logLevel);
        dotNet.AddParameter(logger);
        dotNet.AddParameter(message);
        dotNet.AddParameter(exception);

        dotNet.ActivateOptions();


        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

        hierarchy.Root.AddAppender(dotNet);
        hierarchy.Root.Level = Level.Debug;
        hierarchy.Configured = true;

        //log4net.Config.BasicConfigurator.Configure(dotNet);
        log4net.Config.BasicConfigurator.Configure(hierarchy);

    }
}

然后我在启动时在Global.Asax中运行它。

这在XML文件中运行良好,但在将其移动到代码时,它不再需要工作。

我正在将其转移到代码中,并遵循许多其他方式在SO上执行此操作。 在我的情况下,它们似乎都不起作用。

How to configure log4net programmatically from scratch (no config)

Can you configure log4net in code instead of using a config file?

1 个答案:

答案 0 :(得分:2)

原来有一个错误,但它并没有导致页面出现问题。 使用以下代码进行了无效的转换:

AdoNetAppenderParameter thread = new AdoNetAppenderParameter()
        {
            ParameterName = "@thread",
            DbType = System.Data.DbType.String,
            Size = 255,
            Layout = (IRawLayout)new PatternLayout("%thread")
        };

具体做法是:

Layout = (IRawLayout)new PatternLayout("%thread")

需要转换类型:

RawLayoutConverter rlc = new RawLayoutConverter();

AdoNetAppenderParameter thread = new AdoNetAppenderParameter()
        {
            ParameterName = "@thread",
            DbType = System.Data.DbType.String,
            Size = 255,
            Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%thread"))
        };