当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core 2.0 WebApi全局配置及日志实例

ASP.NET Core 2.0 WebApi全局配置及日志实例

2018年01月25日  | 移动技术网IT编程  | 我要评论

樱桃红了第二部全集,南安黄巢山,会声会影素材下载

最新在将原来写的一些webserivce转换为webapi,直接就用了asp.net core 2.0的框架,在使用中,发现的与原有的asp.net不同的地方,通过搜索已经慢慢解决,记录下来备用。

一、全局配置

在asp.net中,全局变更配置写在web.config中,如下所示

<?xml version="1.0"?>
<configuration>
<connectionstrings>
 <add name="conn" connectionstring="data source=localhost;initial catalog=helloworld;integrated security=true"/>
 </connectionstrings>
 <appsettings>
 <add key="app_key" value="helloworld" />
 <add key="app_secret" value="1234567890abcdef" />
 </appsettings>
</configuration>

在asp.net core 2.0 webapi中,已经没有了web.config文件,查了一些资料,可以把全局变量配置写在appsetting.json文件中,如下所示:

{
 "connectionstrings": {
 "conn": "data source=localhost;initial catalog=helloworld;integrated security=true"
 }
 "appsettings": {
 "app_key": "helloworld",
 "app_secret": "1234567890abcdef"
 }
}

这样一来,在程序中就可以对全局变量配置进行引用了。

使用appsetting.json,全局变量可以设置的更为复杂,具体的方法可以参考文后的参考文献。

二、记录日志

以前asp.net的时候,日志都是用nlog进行记录,现在转换到了core 2.0,也准备继续使用nlog,在使用中,发现和以前的有也所不同。

首先,在nuget中获取nlog.web.aspnetcore包,

然后将startup.cs文件的代码进行修改

public void configure(iapplicationbuilder app, ihostingenvironment env)
//修改为
public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)

并在configure函数中,加上以下语句:

loggerfactory.addnlog();
app.addnlogweb();
loggerfactory.configurenlog(“nlog.config”);

记得要在文件头先引用using nlog.web和using nlog.extensions.logging;

增加一个"web配置文件",文件名为nlog.config,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/nlog.xsd"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
 <targets>
  <target xsi:type="file" name="logfile" filename="${basedir}/logs/${shortdate}.log" keepfileopen="false" layout="${longdate}|${callsite:filename=true}|${uppercase:${level}}|${message} ${exception}" />
  <target xsi:type="file" name="debugfile" filename="${basedir}/logs/${shortdate}_debug.log" keepfileopen="false" layout="${longdate}|${callsite:filename=true}|${uppercase:${level}}|${message} ${exception}" />
 <target xsi:type="file" name="errfile" filename="${basedir}/logs/${shortdate}_error.log" keepfileopen="false" layout="${longdate}|${callsite:filename=true}|${uppercase:${level}}|${message} ${exception}" />
 </targets>
 <rules>
 <logger name="*" level="debug" writeto="debugfile" />
  <logger name="*" level="error" writeto="errfile" />
 <logger name="*" minlevel="trace" writeto="logfile" />
 </rules>
</nlog>

然后在程序中就可以开始调用日志功能了。

二个功能的demo代码如下:

using system;
using system.io;
using microsoft.extensions.configuration;
using nlog.extensions.logging;
using nlog.web;
public class program
{
 public static iconfigurationroot configuration { get; set; }
 public static nlog.logger log = nlog.logmanager.getcurrentclasslogger();
 public static void configandlog()
 {
  var builder = new configurationbuilder()
   .setbasepath(directory.getcurrentdirectory())
   .addjsonfile("appsettings.json");
  configuration = builder.build();
  string app_key = configuration["appsettings:app_key"];
  string coon = configuration["connectionstrings:conn"];
  log.debug("数据库连接为:" + conn);
  return;
 }
}

以上这篇asp.net core 2.0 webapi全局配置及日志实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网