3

您好我正在尝试在不使用 XML 配置的情况下设置 log4net:

    public void InitLogger()
    {
        //Create exception log file if it haven't been created
        var exceptionLogFile = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["ExceptionLogFilePath"]);
        if (!exceptionLogFile.Exists)
        {
            exceptionLogFile.Create();
        }
        //configure log4net
        var smtpAppender = new SmtpAppender
                               {
                                   SmtpHost = "",
                                   Authentication = SmtpAppender.SmtpAuthentication.None,
                                   BufferSize = 512,
                                   From = "",
                                   To = "",
                                   Layout = new PatternLayout(""),
                                   Lossy = true,
                                   Evaluator = new LevelEvaluator(Level.Error)
                               };

        var fileAppender = new FileAppender
                               {
                                   File = System.Configuration.ConfigurationManager.AppSettings["ExceptionLogFilePath"],
                                   AppendToFile = true,
                                   Layout = new PatternLayout(""),
                                   LockingModel = new FileAppender.MinimalLock() //use the minimal locking model that allows multiple processes to write to the same file
                               };


        BasicConfigurator.Configure(smtpAppender);
    }

我的问题是我想使用两个附加程序(smtp 和文件)。使用

BasicConfigurator.Configure(smtpAppender);

我可以设置初始值,但我还需要设置文件附加程序。你知道为什么要这样做吗?

谢谢你的时间 :)

4

1 回答 1

1

在这种情况下,您想要获取记录器的实例,然后添加您的附加程序。之后,只需配置存储库。IE ,

在 VB 中:

Public Class Test  
    Private Shared ReadOnly _logger As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType)         
Sub InitLogger()
        Dim logger = CType(_logger.Logger, log4net.Repository.Hierarchy.Logger)
        Dim fileAppender = New log4net.Appender.FileAppender() With { _
         .File = "", _
         .AppendToFile = True, _
         .Layout = New log4net.Layout.PatternLayout(""), _
         .LockingModel = New log4net.Appender.FileAppender.MinimalLock() _
        }

        logger.AddAppender(fileAppender)

        Dim repository As log4net.Repository.ILoggerRepository = log4net.LogManager.GetRepository()
        log4net.Config.BasicConfigurator.Configure(repository)
    End Sub
End Class

在 C# 中:

public class Test  
{  
    private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType);  
    public void InitLogger()
    {
        var logger = (log4net.Repository.Hierarchy.Logger)_logger.Logger;

        var fileAppender = new log4net.Appender.FileAppender {
            File = "",
            AppendToFile = true,
            Layout = new log4net.Layout.PatternLayout(""),
            LockingModel = new log4net.Appender.FileAppender.MinimalLock()
        };

        logger.AddAppender(fileAppender);

        log4net.Repository.ILoggerRepository repository = log4net.LogManager.GetRepository();
        log4net.Config.BasicConfigurator.Configure(repository);
    }
}
于 2012-08-09T17:13:22.347 回答