增加NLOG支持,关键方法记录日志

This commit is contained in:
falcon 2020-05-28 14:18:41 +08:00
parent f4d0117c6a
commit 219783ced2
7 changed files with 49 additions and 36 deletions

View File

@ -1,14 +1,7 @@
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ReportService.Models; using ReportService.Models;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using System.Text.Json.Serialization;
namespace ReportService.Controllers namespace ReportService.Controllers
{ {
@ -27,10 +20,7 @@ namespace ReportService.Controllers
reportName ??= ""; reportName ??= "";
Response.Cookies.Append("_userCK",$"{{\"uid\":\"{uid}\",\"un\":\"{un}\"}}"); Response.Cookies.Append("_userCK",$"{{\"uid\":\"{uid}\",\"un\":\"{un}\"}}");
Response.Cookies.Append("_initReport",reportName); Response.Cookies.Append("_initReport",reportName);
return View(); this._logger.LogInformation($"Home_Index:uid:{uid}:un:{un}");
}
public IActionResult Privacy() {
return View(); return View();
} }

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ReportService.Database; using ReportService.Database;
@ -11,10 +12,9 @@ namespace ReportService.Controllers.api
[Route("api/[Controller]/[Action]")] [Route("api/[Controller]/[Action]")]
public class ReportApiController:Controller public class ReportApiController:Controller
{ {
private readonly ILogger<HomeController> _logger;
public IWebHostEnvironment Env { get; private set; } public IWebHostEnvironment Env { get; private set; }
public RSDbContext Db { get; set; } public RSDbContext Db { get; set; }
public ILogger Logger { get; set; }
/// <summary> /// <summary>
/// 报表存放位置 /// 报表存放位置
@ -25,8 +25,8 @@ namespace ReportService.Controllers.api
} }
} }
public ReportApiController(ILogger<HomeController> logger,IWebHostEnvironment env,RSDbContext db) { public ReportApiController(ILogger<ReportApiController> logger,IWebHostEnvironment env,RSDbContext db) {
this._logger = logger; this.Logger = logger;
this.Env = env; this.Env = env;
this.Db = db; this.Db = db;
} }
@ -45,6 +45,7 @@ namespace ReportService.Controllers.api
} }
result.Add(file.Name.Substring(0,file.Name.IndexOf('.'))); result.Add(file.Name.Substring(0,file.Name.IndexOf('.')));
} }
this.Logger.LogInformation($"GetReportFiles:{string.Join(',',result.ToArray())}");
return result; return result;
} }
/// <summary> /// <summary>
@ -56,6 +57,7 @@ namespace ReportService.Controllers.api
var htmlFile = Path.Combine(this.ReportPath,fileName + ".html"); var htmlFile = Path.Combine(this.ReportPath,fileName + ".html");
using(var fr = System.IO.File.OpenRead(htmlFile)) { using(var fr = System.IO.File.OpenRead(htmlFile)) {
using(var sr = new StreamReader(fr)) { using(var sr = new StreamReader(fr)) {
this.Logger.LogInformation($"GetHtml:{fileName}");
return Content(sr.ReadToEnd()); return Content(sr.ReadToEnd());
} }
} }
@ -70,6 +72,7 @@ namespace ReportService.Controllers.api
var htmlFile = Path.Combine(this.ReportPath,fileName + ".rpt.html"); var htmlFile = Path.Combine(this.ReportPath,fileName + ".rpt.html");
using(var fr = System.IO.File.OpenRead(htmlFile)) { using(var fr = System.IO.File.OpenRead(htmlFile)) {
using(var sr = new StreamReader(fr)) { using(var sr = new StreamReader(fr)) {
this.Logger.LogInformation($"GetPrint:{fileName}");
return Content(sr.ReadToEnd(),"text/html; charset=utf-8"); return Content(sr.ReadToEnd(),"text/html; charset=utf-8");
} }
} }
@ -82,6 +85,7 @@ namespace ReportService.Controllers.api
/// <returns>json对象</returns> /// <returns>json对象</returns>
public object GetResult(string sql) { public object GetResult(string sql) {
var result = this.Db.SqlJsonQuery(sql); var result = this.Db.SqlJsonQuery(sql);
this.Logger.LogInformation($"GetResult:\n{sql}\n{result}");
return Content(result,"application/json; charset=utf-8"); return Content(result,"application/json; charset=utf-8");
} }

View File

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using NLog.Web;
namespace ReportService namespace ReportService
{ {
@ -20,6 +15,7 @@ namespace ReportService
.ConfigureWebHostDefaults(webBuilder => { .ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
}) })
.UseWindowsService(); .UseWindowsService()
.UseNLog();
} }
} }

View File

@ -19,5 +19,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +0,0 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
<p>Use this page to detail your site's privacy policy.</p>

25
ReportService/nlog.config Normal file
View File

@ -0,0 +1,25 @@
<?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"
autoReload="true">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="logs\log${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<target name="logconsole" xsi:type="Console" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="*" minlevel="Trace" writeTo="logconsole" />
</rules>
</nlog>

View File

@ -12,21 +12,24 @@
<style type="text/css"> <style type="text/css">
@page { @page {
size: A4; size: auto;
} }
</style> </style>
</head> </head>
<body> <body>
<div id="findPerson"> <div id="findPerson" class="table">
<div id="result" v-if="result.length > 0"> <div id="result" v-if="result.length > 0">
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<td>姓名</td> <th colspan="5">报表1</th>
<td>年龄</td> </tr>
<td>性别</td> <tr>
<td>登录用户编号</td> <th>姓名</th>
<td>登录用户名</td> <th>年龄</th>
<th>性别</th>
<th>登录用户编号</th>
<th>登录用户名</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>