Ver código fonte

feat(web):添加日志

Zhenghanjv 8 meses atrás
pai
commit
7e48dd987e

+ 9 - 0
src/FccLife.Web/Controller/ConfigController.cs

@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.RazorPages;
 using System.Drawing;
+using System.Text.Json;
 using System.Xml.Linq;
 
 namespace FccLite.Web.Controller
@@ -16,6 +17,8 @@ namespace FccLite.Web.Controller
     [ApiController]
     public class ConfigController : ControllerBase
     {
+        static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
+
         private readonly IStationService _stationService;
         private readonly IOilInfoService _oilInfoService;
         public ConfigController(IStationService stationService,IOilInfoService oilInfoService) 
@@ -32,6 +35,7 @@ namespace FccLite.Web.Controller
         [HttpPost("upLoadBaseInfo")]
         public async Task<StationSetOutput> UpLoadBaseInfo(StationInfoInput stationInfoInput)
         {
+            logger.Info($"add or update station info,{JsonSerializer.Serialize(stationInfoInput)}");
             return await _stationService.UploadBaseInfo(stationInfoInput);
         }
 
@@ -48,6 +52,7 @@ namespace FccLite.Web.Controller
         [HttpGet("getBaseInfo")]
         public async Task<IActionResult> GetBaseInfo(int page, int size,long? id,string? name, decimal? longitude,decimal? latitude)
         {
+            logger.Info($"get page of station info:page = {page},size = {size}, id = {id},name = {name},longitude = {longitude},latitude = {latitude}");
             var response = await _stationService.GetBaseInfo(page, size, id,name,longitude,latitude);
             return Ok(response);
         }
@@ -60,6 +65,7 @@ namespace FccLite.Web.Controller
         [HttpPost("deleteBaseInfo")]
         public async Task<IActionResult> DeleteBaseInfo(long id)
         {
+            logger.Info($"delete station info by id:{id}");
             var response = await _stationService.DeleteBaseInfo(id);
             return Ok(response);
 
@@ -77,6 +83,7 @@ namespace FccLite.Web.Controller
         [HttpGet("getOilInfo")]
         public async Task<IActionResult> GetOilInfo(int page,int size,long? id,string? name,int? code)
         {
+            logger.Info($"get page of oil info,page = {page},size = {size},id = {id},name = {name},code = {code}");
             var response = await _oilInfoService.GetPage(page, size, id, name, code);
             return Ok(response);
         }
@@ -89,6 +96,7 @@ namespace FccLite.Web.Controller
         [HttpPost("upLoadOilInfo")]
         public async Task<OilInfoSetOutput> UpLoadOilInfo(OilInfoInput oilInfoInput)
         {
+            logger.Info($"add or update oil info,{JsonSerializer.Serialize(oilInfoInput)}");
             return await _oilInfoService.Save(oilInfoInput);
         }
 
@@ -100,6 +108,7 @@ namespace FccLite.Web.Controller
         [HttpPost("deleteOilInfo")]
         public async Task<IActionResult> DeleteOilInfo(long id)
         {
+            logger.Info($"delete oil info by id:{id}");
             var response = await _oilInfoService.DeleteById(id);
             return Ok(response);
         }

+ 7 - 0
src/FccLife.Web/FccLite.Web.csproj

@@ -8,8 +8,15 @@
 
   <ItemGroup>
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" />
+    <PackageReference Include="NLog" Version="5.3.4" />
     <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
   </ItemGroup>
 
+  <ItemGroup>
+    <Content Update="nlog.config">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+  </ItemGroup>
+
 </Project>

+ 0 - 2
src/FccLife.Web/Program.cs

@@ -12,8 +12,6 @@ builder.Services.AddDbContext<MysqlDbContext>(options =>
     options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection")))
     .LogTo(Console.WriteLine,LogLevel.Debug));
 
-
-
 //controller
 builder.Services.AddControllers();
 

+ 17 - 4
src/FccLife.Web/Repositories/FccOilInfo/OilInfoReposity.cs

@@ -7,6 +7,7 @@ namespace FccLite.Web.Repositories.FccOilInfo
 {
     public class OilInfoReposity : IOilInfoReposity
     {
+        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
         private readonly MysqlDbContext _dbContext;
         public OilInfoReposity(MysqlDbContext mysqlDbContext) 
         {
@@ -21,7 +22,11 @@ namespace FccLite.Web.Repositories.FccOilInfo
         public async Task<bool> DeleteByID(long id)
         {
             Domain.FccOilInfo.FccOilInfo? info = await _dbContext.OilInfos.FindAsync(id);
-            if (info == null) return false;
+            if (info == null)
+            {
+                Logger.Info($"find oil info with id:{id} for delete,result is null");
+                return false;
+            }
             _dbContext.OilInfos.Remove(info);
             await _dbContext.SaveChangesAsync();
             return true;
@@ -52,13 +57,21 @@ namespace FccLite.Web.Repositories.FccOilInfo
         /// </summary>
         /// <param name="oilInfoInput"></param>
         /// <returns></returns>
-        public async Task<Domain.FccOilInfo.FccOilInfo> UploadOilInfo(OilInfoInput? oilInfoInput)
+        public async Task<Domain.FccOilInfo.FccOilInfo?> UploadOilInfo(OilInfoInput? oilInfoInput)
         {
-            if (oilInfoInput == null) return null;
+            if (oilInfoInput == null)
+            {
+                Logger.Info($"oilInfoInput is null");
+                return null;
+            }
             if(oilInfoInput.Id != null)
             {
                 Domain.FccOilInfo.FccOilInfo? info = await _dbContext.OilInfos.FindAsync(oilInfoInput.Id);
-                if (info == null) return null;
+                if (info == null)
+                {
+                    Logger.Info($"find oil info with id: {oilInfoInput.Id} for update,result is null");
+                    return null;
+                }
                 info.Name = oilInfoInput.Name;
                 info.Code = oilInfoInput.Code;
                 info.Price = oilInfoInput.Price;

+ 18 - 4
src/FccLife.Web/Repositories/FccStationInfo/StationRepository.cs

@@ -4,12 +4,15 @@ using FccLite.Web.utils.database;
 using Microsoft.AspNetCore.Cors.Infrastructure;
 using Microsoft.EntityFrameworkCore;
 using System.Net;
+using System.Text.Json;
 using System.Xml.Linq;
 
 namespace FccLite.Web.Repositories.FccStationInfo
 {
     public class StationRepository : IStationRepository
     {
+        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
+
         private readonly MysqlDbContext _dbContext;
         public StationRepository(MysqlDbContext dbContext)
         {
@@ -58,13 +61,20 @@ namespace FccLite.Web.Repositories.FccStationInfo
         public async Task<Domain.FccStationInfo.FccStationInfo?> Save(StationInfoInput stationInfoInput)
         {
            
-            if (stationInfoInput == null) return null;
-            if (stationInfoInput.IsNotNorm()) return null;
+            if (stationInfoInput == null || stationInfoInput.IsNotNorm())
+            {
+                Logger.Info($"stationInput not norm,it is {JsonSerializer.Serialize(stationInfoInput)}");
+                return null;
+            }
             if (stationInfoInput.Id != null)
             {
                 Domain.FccStationInfo.FccStationInfo? station = await _dbContext.FccStationInfos.FindAsync(stationInfoInput.Id);
 
-                if (station == null) return null;
+                if (station == null)
+                {
+                    Logger.Info($"find station info with id:{stationInfoInput.Id} for update,result is null");
+                    return null;
+                }
 
                 station.BuildId = stationInfoInput.BuildId;
                 station.Name = stationInfoInput.Name != null ? stationInfoInput.Name : "";
@@ -93,7 +103,11 @@ namespace FccLite.Web.Repositories.FccStationInfo
         public async Task<bool> DeleteByID(long id)
         {
             Domain.FccStationInfo.FccStationInfo? info = await _dbContext.FccStationInfos.FindAsync(id);
-            if (info == null) return false;
+            if (info == null)
+            {
+                Logger.Info($"find station info with id:{id} for delete,result is null");
+                return false;
+            }
             _dbContext.FccStationInfos.Remove(info);
             _dbContext.SaveChanges();
             return true;

+ 5 - 0
src/FccLife.Web/Services/FccOilInfo/OilInfoServiceImpl.cs

@@ -1,11 +1,13 @@
 using FccLite.Web.Domain.FccOilInfo.Input;
 using FccLite.Web.Domain.FccOilInfo.Ouput;
 using FccLite.Web.Repositories.FccOilInfo;
+using System.Text.Json;
 
 namespace FccLite.Web.Services.FccOilInfo
 {
     public class OilInfoServiceImpl : IOilInfoService
     {
+        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
         private readonly IOilInfoReposity _oilInfoReposity;
         public OilInfoServiceImpl(IOilInfoReposity oilInfoReposity) 
         {
@@ -20,6 +22,7 @@ namespace FccLite.Web.Services.FccOilInfo
         public async Task<OilInfoSetOutput> DeleteById(long id)
         {
             bool result = await _oilInfoReposity.DeleteByID(id);
+            Logger.Info($"delete oil info by id result:{result}");
             if (result)
             {
                 return new OilInfoSetOutput()
@@ -59,6 +62,7 @@ namespace FccLite.Web.Services.FccOilInfo
                 }
             ).ToList();
 
+            Logger.Info($"get page of oil info,count is {oilInfos.Count}");
             return new OilInfoOutput()
             {
                 Total = oilInfos.Count,
@@ -74,6 +78,7 @@ namespace FccLite.Web.Services.FccOilInfo
         public async Task<OilInfoSetOutput> Save(OilInfoInput oilInfoInput)
         {
             Domain.FccOilInfo.FccOilInfo fccOilInfo = await _oilInfoReposity.UploadOilInfo(oilInfoInput);
+            Logger.Info($"add or update oil info,{JsonSerializer.Serialize(fccOilInfo)}");
             if (fccOilInfo == null)
             {
                 return new OilInfoSetOutput()

+ 6 - 0
src/FccLife.Web/Services/FccStaionInfo/StationServiceImpl.cs

@@ -1,11 +1,14 @@
 using FccLite.Web.Domain.FccStationInfo.Input;
 using FccLite.Web.Domain.FccStationInfo.Output;
 using FccLite.Web.Repositories.FccStationInfo;
+using System.Text.Json;
 
 namespace FccLite.Web.Services.FccStaionInfo
 {
     public class StationServiceImpl : IStationService
     {
+        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
+
         private readonly IStationRepository _stationRepository;
         public StationServiceImpl(IStationRepository stationRepository)
         {
@@ -42,6 +45,7 @@ namespace FccLite.Web.Services.FccStaionInfo
                     WebSocketPort = stationInfo.WebSocketPort
                 }
             ).ToList();
+            Logger.Info($"get staion info,count is {fccStationInfos.Count}");
             return new StationInfoOutput()
             {
                 Total = fccStationInfos.Count,
@@ -57,6 +61,7 @@ namespace FccLite.Web.Services.FccStaionInfo
         public async Task<StationSetOutput> UploadBaseInfo(StationInfoInput stationInfoInput)
         {
             Domain.FccStationInfo.FccStationInfo info = await _stationRepository.Save(stationInfoInput);
+            Logger.Info($"add or update result: {JsonSerializer.Serialize(info)}");
             if (info == null)
             {
                 return new StationSetOutput()
@@ -82,6 +87,7 @@ namespace FccLite.Web.Services.FccStaionInfo
         public async Task<StationSetOutput> DeleteBaseInfo(long id)
         {
             Boolean result = await _stationRepository.DeleteByID(id);
+            Logger.Info($"deleted station info by id:{id},result = {result}");
             if(result)
             {
                 return new StationSetOutput()

+ 23 - 0
src/FccLife.Web/nlog.config

@@ -0,0 +1,23 @@
+<?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">
+
+	<!-- 定义日志级别和规则 -->
+	<extensions>
+		<add assembly="NLog.Web.AspNetCore"/>
+	</extensions>
+
+	<targets>
+		<!-- 将日志写入文件 -->
+		<target xsi:type="File" name="file" archiveAboveSize="20480" archiveEvery="Day" archiveNumbering="Sequence" maxArchiveFiles="7" fileName="${basedir}/logs/nlog-${shortdate}.log"
+		layout="${date:format=HH\:mm\:ss.fff} [${threadid}:${level:uppercase=true}]${logger} - ${message} ${exception}" />
+
+		<!-- 将日志写入控制台 -->
+		<target xsi:type="Console" name="console" layout="${date:format=HH\:mm\:ss.fff} [${threadid}:${level:uppercase=true}]${logger} - ${message} ${exception}" />
+	</targets>
+
+	<rules>
+		<!-- 捕获所有级别的日志并将其写入文件和控制台 -->
+		<logger name="*" minlevel="Info" writeTo="file,console" />
+	</rules>
+</nlog>