7

I'm implementing log4net AdoNetAppender in asp.net core 2.0, but I guess it is not supporting. I have implemented log4net RollingFileAppender in core 2.0 & it worked successfully using 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?

Thank you

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Vishal G
  • 179
  • 4
  • 12

2 Answers2

9

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

prashant
  • 2,181
  • 2
  • 22
  • 37
  • 2
    using 3d party tool to using 3d party tool – Pr.Dumbledor Mar 02 '18 at 15:00
  • 6
    @Pr.Dumbledor Its an answer which solves the problem which Vishal is facing. I have given other option as well. – prashant Mar 04 '18 at 01:57
  • it seems nice but seems lacking some features. like i wanted to filterout some message on base of certain property but not able to succeed . check https://github.com/microknights/Log4NetAdoNetAppender/issues/13 – Kamran Shahid Jan 15 '20 at 13:29
  • A followup on @KamranShahid comment. The Log4NetAdoNetAppender for .net core/standard is working just fine. However If you need to do filters on custom fields from the appender, you need to write a custom filter. The is general for log4net, and the recipe can be found in the provided link – Frank Nielsen Jan 23 '20 at 22:23
0

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.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • log4net.Appender.AdoNetAppender is not supported in .net core 2.0 until now. – prashant Feb 18 '18 at 23:46
  • @prashant what are you talking about? I'm using this code in project that targeting to .netcoreapp2.0 but of course package of log4net is targeting on 461. Everything works fine with that workaround – Roman Marusyk Feb 19 '18 at 00:26
  • @RomanMarusyk Thanks works fine if you edit your answer maybe @ prashant will know what do you mean, I had to add AdoNetAppender Class and call it from my own application and your code works perfectly – khaled Dehia Mar 13 '19 at 01:13
  • You can do the same as what id proposed here by adding to the xml file. connectionStringName is as noted useless – Ant Jul 02 '21 at 10:18