简体   繁体   English

不支持.Net core 2.0中的log4net AdoNetAppender?

[英]log4net AdoNetAppender in .Net core 2.0 not supported?

I'm implementing log4net AdoNetAppender in asp.net core 2.0 , but I guess it is not supporting. 我正在asp.net core 2.0中实现log4net AdoNetAppender ,但我猜它不支持。 I have implemented log4net RollingFileAppender in core 2.0 & it worked successfully using log4.net config. 我已经在核心2.0中实现了log4net RollingFileAppender ,它使用log4.net config成功运行。 So, if log4net AdoNetAppender is not supporting in core 2.0, is there any other way to insert logs to sql database in core 2.0? 那么,如果log4net AdoNetAppender在核心2.0中不支持,还有其他方法可以将日志插入到核心2.0中的sql数据库吗?

Thank you 谢谢

I faced the same issue and solved using this nuget package in .net core solution 我遇到了同样的问题并解决了在.net核心解决方案中使用这个nuget包的问题

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

You can find more information about how to set this up on 您可以找到有关如何进行此设置的详细信息

https://github.com/microknights/Log4NetAdoNetAppender 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 其他选项可以参考https://svn.apache.org/repos/asf/logging/log4net/tags/log4net-1_2_9/src/Appender/AdoNetAppender.cs中的实现

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. 使用appender将log4net.config.xml文件添加到ASP.NET Core项目。 In this file for AdoNetAppender you can specify connectionString or connectionStringName but it doesn't make sense because the connection will be null . AdoNetAppender的此文件中,您可以指定connectionStringconnectionStringName但它没有意义,因为连接将为null

So, add connection string to appsettings.json instead 因此,请将连接字符串添加到appsettings.json

"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. ActivateOptions()调用重新初始化appender。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM