博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]Setting the NLog database connection string in the ASP.NET Core appsettings.json
阅读量:6543 次
发布时间:2019-06-24

本文共 2231 字,大约阅读时间需要 7 分钟。

本文转自:

NLog posts in this series:

  1. Settings the NLog database connection string in the ASP.NET Core appsettings.json

The XML nlog.config file is the same as in the previous , with no database connection string configured.

insert into dbo.Log ( Application, Logged, Level, Message, Logger, CallSite, Exception ) values ( @Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception );

The NLog DatabaseTarget connectionstring is configured in the appsettings.json as described in the ASP.NET Core configuration docs.

{    "Logging": {        "IncludeScopes": false,        "LogLevel": {            "Default": "Debug",            "System": "Information",            "Microsoft": "Information"        }    },    "ElasticsearchUrl": "http://localhost:9200",    "ConnectionStrings": {        "NLogDb": "Data Source=N275\\MSSQLSERVER2014;Initial Catalog=Nlogs;Integrated Security=True;"    }}

The configuration is then read in the Startup constructor.

public Startup(IHostingEnvironment env){	var builder = new ConfigurationBuilder()		.SetBasePath(env.ContentRootPath)		.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)		.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)		.AddEnvironmentVariables();	Configuration = builder.Build();}

The Nlog DatabaseTagert is then configured to use the connection string from the app settings and sets all the DatabaseTarget instances for NLog to use this. All target properties can be configured in this way if required.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){	loggerFactory.AddNLog();	foreach (DatabaseTarget target in LogManager.Configuration.AllTargets.Where(t => t is DatabaseTarget))	{		target.ConnectionString = Configuration.GetConnectionString("NLogDb");	}		LogManager.ReconfigExistingLoggers();		app.UseMvc();}

Links

 

转载地址:http://hcodo.baihongyu.com/

你可能感兴趣的文章
git fetch & pull详解
查看>>
优酷2013.3去广告 不黑屏
查看>>
web入门、tomcat、servlet、jsp
查看>>
boost_1.63.0编译VS2013
查看>>
mysql查看每个数据库所占磁盘大小
查看>>
jQuery 插件-(初体验一)
查看>>
PHP语言 -- Ajax 登录处理
查看>>
基于js的CC攻击实现与防御
查看>>
Largest Rectangle in a Histogram
查看>>
树状数组模板
查看>>
我的家庭私有云计划-19
查看>>
项目实践中Linux集群的总结和思考
查看>>
关于使用Android NDK编译ffmpeg
查看>>
监控MySQL主从同步是否异常并报警企业案例模拟
查看>>
zabbix从2.2.3升级到最新稳定版3.2.1
查看>>
我有一个网站,想提高点权重
查看>>
2017年前端框架、类库、工具大比拼
查看>>
浅谈(SQL Server)数据库中系统表的作用
查看>>
微软邮件系统Exchange 2013系列(七)创建发送连接器
查看>>
程序员杂记系列
查看>>