温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET Core 2.0中WebApi全局配置及日志的示例分析

发布时间:2021-08-05 14:39:28 来源:亿速云 阅读:141 作者:小新 栏目:开发技术

小编给大家分享一下ASP.NET Core 2.0中WebApi全局配置及日志的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

一、全局配置

在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全局配置及日志的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI