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

所属分类: 网络编程 / ASP.NET 阅读数: 588
收藏 0 赞 0 分享

最新在将原来写的一些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全局配置及日志实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

开源跨平台运行服务插件TaskCore.MainForm

这篇文章主要为大家详细介绍了开源跨平台运行服务插件TaskCore.MainForm的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

CKEditor自定义按钮插入服务端图片

这篇文章主要为大家详细介绍了CKEditor自定义按钮插入服务端图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net Web Api实现图片点击式图片验证码功能

现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码。下面通过本文给大家分享Asp.net Web Api实现图片点击式图片验证码功能,需要的的朋友参考下吧
收藏 0 赞 0 分享

WPF实现ScrollViewer滚动到指定控件处

这篇文章主要为大家详细介绍了WPF实现ScrollViewer滚动到指定控件处,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现带全选复选框的列表控件

这篇文章主要为大家详细介绍了WPF实现带全选复选框的列表控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net MVC 中利用jquery datatables 实现数据分页显示功能

这篇文章主要介绍了Asp.net MVC 中利用jquery datatables 实现数据分页显示功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net 利用NPOI导出Excel通用类的方法

本篇文章主要介绍了asp.net 利用NPOI导出Excel通用类的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2015自带LocalDB数据库用法详解

这篇文章主要为大家详细介绍了VS2015自带LocalDB数据库的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(一)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(二)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务的第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多