NLog with Entity Framework Connection String?

I am trying to use the 'Database' NLog target and would like NLog to read my connection string so as not to set it twice in the same configuration file. The problem is that I have an Entity Framework connection string , so using the attribute connectionStringNamedoes not work.

In log4net, I can use a custom AdoNetAppender and independently extract the corresponding fragments of the connection string. Is there a way to configure the target NLog database so that I can jump to a line with a line of the appropriate style?

+7
source share
5 answers

, :

    private DatabaseTarget CreateDatabaseTarget()
    {
        var entityFrameworkConnection = ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString;
        var builder = new EntityConnectionStringBuilder(entityFrameworkConnection);
        var connectionString = builder.ProviderConnectionString;

        var target = new DatabaseTarget()
        {
            ConnectionString = connectionString,
            CommandText = @"insert into Log ([DateTime], [Message]) values (@dateTime, @message);",
            Parameters = {
                new DatabaseParameterInfo("@dateTime", new NLog.Layouts.SimpleLayout("${date}")), 
                new DatabaseParameterInfo("@message", new NLog.Layouts.SimpleLayout("${message}")), 
           }
        };
        return target;
    }

NLog:

var target = CreateDatabaseTarget();
LogManager.Configuration.AddTarget("databaseTarget", CreateDatabaseTarget());
LogManager.Configuration.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Warn, target));

Nuget , NLog.Mvc NLog.EntityFramework , nuget...

+7

DatabaseTarget, sealed. , DatabaseTarget.InitializeTarget() , DbProviderFactory EF providerName="System.Data.EntityClient"

, NLog 2.0.0.0 :

+5

DbContext :

context.Database.Connection.ConnectionString

, , , :

1)

 <target name="database" type="Database" connectionString="${gdc:myConnectionString}">
    <commandText>
      ...
    </commandText>

2)

GlobalDiagnosticsContext.Set("myConnectionString", connectionString);

, db.

+1

NLog.config

<variable name="connectionStringNLog" value="" />

<target name="my-db"
        xsi:type="Database"
        dbProvider="System.Data.SqlClient"        
        connectionString="${var:connectionStringNLog}"
        commandType="StoredProcedure"
        commandText="[dbo].[LogEntryInsert]">

# , , , :

LogManager.Configuration.Variables["connectionStringNLog"] = conn.ConnectionString;
0

NLog 4.5 adds support for parsing connection strings from the Entity Framework, so you can use them directly:

https://github.com/NLog/NLog/pull/2510

In addition, the NLog DatabaseTarget is no longer sealed, so if special handling is required, you can also do this.

The usual way to do special parsing- / lookup-logic is to register a custom layoutrenderer and reference it in ConnectionString-Layout:

https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer

0
source

All Articles