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

View File

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

View File

@ -19,5 +19,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />
</ItemGroup>
</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">
@page {
size: A4;
size: auto;
}
</style>
</head>
<body>
<div id="findPerson">
<div id="findPerson" class="table">
<div id="result" v-if="result.length > 0">
<table class="table">
<thead>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>登录用户编号</td>
<td>登录用户名</td>
<th colspan="5">报表1</th>
</tr>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>登录用户编号</th>
<th>登录用户名</th>
</tr>
</thead>
<tbody>