nlog 的手动配置

时间:2022-04-16 06:12:28

使用nlog的时候,有时候需要手动配置。比如数据库链接和密码不配在文件里,或者统计配置在一个位置之类的。

var config = new NLog.Config.LoggingConfiguration( );

        var logfile = new NLog.Targets.DatabaseTarget( ) {  };

        logfile.CommandText = "        insert into MyLog ([CreateDate], [Origin], [LogLevel], [Message], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @stackTrace);";

        logfile.CommandType = System.Data.CommandType.Text;

        logfile.ConnectionString = "";

        logfile.Name="dblog";

        logfile.Parameters.Add( new DatabaseParameterInfo( "@createDate", "${longdate}" ) );

        logfile.DBProvider = "MySql.Data.MySqlClient";//System.Data.SqlClient

        config.LoggingRules.Add( new NLog.Config.LoggingRule( "*", NLog.LogLevel.Debug, logfile ) );

        NLog.LogManager.Configuration = config;

        loggerFactory.AddNLog( );