master
bicijinlian 6 years ago
parent 6a101ee411
commit 84412c7644

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace NLogStudy.WebCore2.Study.Controllers namespace NLogStudy.WebCore2.Study.Controllers
{ {
@ -10,10 +11,16 @@ namespace NLogStudy.WebCore2.Study.Controllers
[ApiController] [ApiController]
public class ValuesController : ControllerBase public class ValuesController : ControllerBase
{ {
private readonly ILogger logger;
public ValuesController(ILogger<ValuesController> logger)
{
this.logger = logger;
}
// GET api/values // GET api/values
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<string>> Get() public ActionResult<IEnumerable<string>> Get()
{ {
logger.LogError("我是测试错误");
return new string[] { "value1", "value2" }; return new string[] { "value1", "value2" };
} }

File diff suppressed because it is too large Load Diff

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
@ -8,6 +8,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="NLog" Version="4.6.7" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NLog.Web;
namespace NLogStudy.WebCore2.Study namespace NLogStudy.WebCore2.Study
{ {
public class Program public class Program
@ -19,6 +21,13 @@ namespace NLogStudy.WebCore2.Study
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args) WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>(); .UseStartup<Startup>()
.ConfigureLogging((hostBuild,logBuild)=> {
//清除自带的Providers
logBuild.ClearProviders();
//默认最小级别
logBuild.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug);
})
.UseNLog();
} }
} }

@ -0,0 +1,41 @@
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
throwConfigExceptions="true"
internalLogLevel="Info"
internalLogFile="${basedir}\logs\internal-nlog.txt"
>
<!--引入Nlog扩展包-->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!--自定义变量-->
<variable name="myvariable" value="test" />
<!--设置目标-->
<targets>
<!--默认彩色控制台-->
<target xsi:type="ColoredConsole" name="DefaultColoredConsole"/>
<!--写入日志文件-->
<target xsi:type="File"
name="allfile"
fileName="${basedir}\logs\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- 写入文件使用asp.net core -->
<target xsi:type="File"
name="ownFile-web"
fileName="${basedir}\logs\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>
<!--设置规则-->
<rules>
<logger ruleName="*" minlevel="Trace" writeTo="DefaultColoredConsole" />
</rules>
</nlog>

@ -1 +1,7 @@
# Nlogѧϰ # Nlog学习
## 一、配置文件智能提示
方法-
> 引入Nuget包:NLog.config 和 NLog.Schema
方法二:
> 下载提示文件 https://nlog-project.org/schemas/NLog.xsd,放入解决方案根目录
> nlog.config文件 nlog根节点加入 xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" 属性

Loading…
Cancel
Save