Log4NET AdoNetAppender Connection String Link

I want to use Log4NET to enter my database and after some tutorials, I see that I need to configure the connection string for the DAO appender.

I already have a connection string in my web.config for connecting to the database, so I'm wondering if I can refer to this instead of installing a new one.

I usually change the connection string to switch from Dev DB to the production database, so it would be very useful to use the same connection parameters.

+4
source share
1 answer

Edit: since this was published, now log4net supports specifying the connection string name in the configuration. From https://issues.apache.org/jira/browse/LOG4NET-88 -

This adds the new connectionStringName property (and the corresponding ConnectionStringName property) to the AdoNetAppender class. This is a link to the connection string in the <ConnectionStrings> section of the App.config or Web.config file.

Here is an example in this answer .

If you do not have the connection string defined in the <ConnectionStrings> section, you can set the connection string at runtime using this class:

You call SetConnectionString using the connection string immediately after setting up log4net.

However, there are a few things I would like to point out:

Log4net database subscribers do not like to create without a connection string and cause an (internal) error - and during unit testing, I saw that the tests take more than 10 seconds when they are assigned a false connection string.

 public static class LogConfigurator { public static void SetConnectionString(string connectionString) { Hierarchy logHierarchy = log4net.LogManager.GetRepository() as Hierarchy; if (logHierarchy == null) { throw new InvalidOperationException ("Can't set connection string as hierarchy is null."); } var appender = logHierarchy.GetAppenders() .OfType<AdoNetAppender>() .SingleOrDefault(); if (appender == null) { throw new InvalidOperationException ("Can't locate a database appender"); } appender.ConnectionString = connectionString; appender.ActivateOptions(); } } 
+6
source

Source: https://habr.com/ru/post/1447699/


All Articles