Castle和NLog在运行时更改连接字符串 [英] Castle and NLog change connection string at runtime

查看:147
本文介绍了Castle和NLog在运行时更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的对城堡的支持的NLog,并试图找到一种在运行时更改连接字符串的方法.

i am using the NLog built in support for castle and trying to find a way to alter the connection string at run time.

这是我最近的一次尝试,因为我的所有配置都为空,因此我确信它必须完成生命周期,所以我猜测城堡还没有连接NLog上的勇气.

this is my latest swing and miss, im sure it has to do the life cycle at this point as all of the configuration is null so i am guessing that castle has not yet wired up the guts on NLog.

    private const string NLogConnectionString = "NLogConnection";

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<LoggingFacility>(l => l.UseNLog());

        var config = new NLog.Config.LoggingConfiguration();
        var dbtarget = config.FindTargetByName("database") as DatabaseTarget;

        if (dbtarget != null)
        {
            dbtarget.ConnectionString = MethodThatGiveMeConnectionString(NLogConnectionString);
        }
    }

查看这篇文章,它可能是一个选择但是基于此处的处理方式,我不想更改它,而更喜欢仅将连接字符串直接提供给NLog.

looking at this post it could be an option but based on the way things have been done here i dont want to change that and much prefer just providing the connection string directly to NLog.

看这里我知道我可以在以下位置进行配置运行时,但我更希望让大多数设置来自配置文件,然后覆盖连接字符串.

looking here I know i can configure this at run time but i much prefer let most of the settings come from the config file and then just override the connection string.

推荐答案

所以我找到了一个可行的解决方案,但不确定是否是最佳方法.

So I found a solution that works but not sure it's the best approach.

使用这篇文章作为参考.此评论最有帮助:

Using this post as a reference. This comment was the most helpful:

最初,我尝试实现一个自定义ILoggerFactory并将其通过customLoggerFactory注入到LoggingFacility中.但这很快就被证明是死胡同.然后,我查看了NLog集成,发现已经有一个NLogFactory,其方法标记为virtual.这样我就可以从此类中衍生

Initially I tried to implement a custom ILoggerFactory and inject it into LoggingFacility via the customLoggerFactory. But that soon proved to be a dead end. Then I looked into the NLog integration and noticed that there's already a NLogFactory that has it's methods marked as virtual. So I was able to derive from this class

作者要解决的问题与我自己的问题不同,但这使我想出了以下解决方案:

The problem the author is solving is different than my own but it got me to come up with this solution:

public class LoggerInstall : IWindsorInstaller
{
    private const string NLogConnectionString = "NLogConnection";

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        var config = new NLog.Config.LoggingConfiguration();
        container.AddFacility<LoggingFacility>(l => l.LogUsing(new OwnNLogFactory(GetYourConnectionStringMethod(NLogConnectionString))));
    }
}


public class OwnNLogFactory : NLogFactory  
{
    public OwnNLogFactory(string connectionString)
    {
        foreach (var dbTarget in LogManager.Configuration.AllTargets.OfType<DatabaseTarget>().Select(aTarget => aTarget))
        {
            dbTarget.ConnectionString = connectionString;
        }
    }
}

仍然不确定这是最好的解决方案,但它目前仍有效,如果有人有,希望查看其他解决方案

Still not sure this is the best solution but it works for now, would love to see other solutions if anyone has one

这篇关于Castle和NLog在运行时更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆