Creating a database programatically in nlog to enable using DatabaseTarget

我只是一个虾纸丫 提交于 2019-12-04 04:39:43

问题


I'm creating a DatabaseTarget object in C# and using it to log data into an NLog database.

If the database does not exist, the nlog target fails. I want to check to see if the DB exists, and if it doesn't create it and a log table.

I can see the targetDB.Install(installationContext) function appears to be able to do the job but cannot find any examples. There are numerous examples using a config file. I want to put this in code and not have to deploy a config file in all applications that use the DLL that will contain this code.

How do I check for and create the database?


回答1:


This code uses the Install() method to create a logging DB and table if they do not already exist:

private static void GetDBLogger(string strConnectionString)
    {
        StringBuilder sb = new StringBuilder();
        InstallationContext installationContext = new InstallationContext();

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.ConnectionString = strConnectionString;
        string strDatabase = builder.InitialCatalog;

        NLog.Targets.DatabaseTarget targetDB = new NLog.Targets.DatabaseTarget();

        targetDB.Name = "db";
        targetDB.ConnectionString = strConnectionString;

        NLog.Targets.DatabaseParameterInfo paramDB;

        paramDB = new NLog.Targets.DatabaseParameterInfo();
        paramDB.Name = string.Format("@Message");
        paramDB.Layout = string.Format("${{message}}");
        targetDB.Parameters.Add(paramDB);
        targetDB.CommandText = string.Format("INSERT INTO Logs(Message) VALUES (@message);");

        // Keep original configuration
        LoggingConfiguration config = LogManager.Configuration;
        if (config == null)
            config = new LoggingConfiguration();

        config.AddTarget(targetDB.Name, targetDB);

        LoggingRule rule = new LoggingRule("*", LogLevel.Debug, targetDB);
        config.LoggingRules.Add(rule);

        LogManager.Configuration = config;

        SqlConnectionStringBuilder builder2 = new SqlConnectionStringBuilder();
        builder2.ConnectionString = strConnectionString;
        builder2.InitialCatalog = "master";

        // we have to connect to master in order to do the install because the DB may not exist
        targetDB.InstallConnectionString = builder2.ConnectionString;

        sb.AppendLine(string.Format("IF NOT EXISTS (SELECT name FROM master.sys.databases WHERE name = N'{0}')", strDatabase));
        sb.AppendLine(string.Format("CREATE DATABASE {0}", strDatabase));

        DatabaseCommandInfo createDBCommand = new DatabaseCommandInfo();
        createDBCommand.Text = sb.ToString();
        createDBCommand.CommandType = System.Data.CommandType.Text;
        targetDB.InstallDdlCommands.Add(createDBCommand);

        // create the database if it does not exist
        targetDB.Install(installationContext);

        targetDB.InstallDdlCommands.Clear();
        sb.Clear();
        sb.AppendLine("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND  TABLE_NAME = 'Logs')");
        sb.AppendLine("RETURN");
        sb.AppendLine("");
        sb.AppendLine("CREATE TABLE [dbo].[Logs](");
        sb.AppendLine("[LogId] [int] IDENTITY(1,1) NOT NULL,");
        sb.AppendLine("[Message] [nvarchar](max) NULL,");
        sb.AppendLine(" CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED ");
        sb.AppendLine("(");
        sb.AppendLine("[LogId] ASC");
        sb.AppendLine(")WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]");
        sb.AppendLine(") ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");

        DatabaseCommandInfo createTableCommand = new DatabaseCommandInfo();
        createTableCommand.Text = sb.ToString();
        createTableCommand.CommandType = System.Data.CommandType.Text;
        targetDB.InstallDdlCommands.Add(createTableCommand);

        // we can now connect to the target DB
        targetDB.InstallConnectionString = strConnectionString;

        // create the table if it does not exist
        targetDB.Install(installationContext);
    }


来源:https://stackoverflow.com/questions/20101809/creating-a-database-programatically-in-nlog-to-enable-using-databasetarget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!