log4net AdoNetAppender in .Net core 2.0 not supported?

后端 未结 2 507
情歌与酒
情歌与酒 2021-01-12 10:01

I\'m implementing log4net AdoNetAppender in asp.net core 2.0, but I guess it is not supporting. I have implemented log4net RollingF

相关标签:
2条回答
  • 2021-01-12 10:43

    I faced the same issue and solved using this nuget package in .net core solution

    https://www.nuget.org/packages/MicroKnights.Log4NetAdoNetAppender

    You can find more information about how to set this up on

    https://github.com/microknights/Log4NetAdoNetAppender

    Other option could be referring to the implementation in https://svn.apache.org/repos/asf/logging/log4net/tags/log4net-1_2_9/src/Appender/AdoNetAppender.cs

    0 讨论(0)
  • 2021-01-12 10:44

    I had the same problem. I've fixed it in the following way:

    Add log4net.config.xml file to the ASP.NET Core project with appenders. In this file for AdoNetAppender you can specify connectionString or connectionStringName but it doesn't make sense because the connection will be null.

    So, add connection string to appsettings.json instead

    "ConnectionStrings": {
       "Log": "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Log.Database;User ID=sa;Password=;MultipleActiveResultSets=True"
    }
    

    Then configure

    ILoggerRepository logRepository = log4net.LogManager.GetRepository(Assembly.GetExecutingAssembly());
    XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));\
    

    and assign connection string manually

    ILog _databaseLogger = log4net.LogManager.GetLogger("DBLogger");
    var repository = _databaseLogger?.Logger.Repository;
    if (repository != null) 
    {
        _adoAppender = repository.GetAppenders()
            .FirstOrDefault(a => a is AdoNetAppender) as AdoNetAppender;
    
        if (_adoAppender != null && string.IsNullOrEmpty(_adoAppender.ConnectionStringName)) 
        {
            _adoAppender.ConnectionString = "some connection string from appsettings.json";
            _adoAppender.ActivateOptions();
        }
    }
    

    The ActivateOptions() calls for reinitialize the appender.

    0 讨论(0)
提交回复
热议问题