Skip to content

Commit 1d56c30

Browse files
authored
使用NLog替换原先的日志工具 (Richasy#60)
1 parent ebebc5a commit 1d56c30

7 files changed

Lines changed: 24 additions & 33 deletions

File tree

src/App/App.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<AppxBundle>Always</AppxBundle>
2525
<AppxBundlePlatforms>arm64</AppxBundlePlatforms>
2626
<HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks>
27-
<PackageCertificateKeyFile />
27+
<PackageCertificateKeyFile>App_TemporaryKey.pfx</PackageCertificateKeyFile>
2828
</PropertyGroup>
2929
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
3030
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
@@ -1060,6 +1060,9 @@
10601060
</ItemGroup>
10611061
<ItemGroup>
10621062
<None Include="App_TemporaryKey.pfx" />
1063+
<Content Include="NLog.config">
1064+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1065+
</Content>
10631066
</ItemGroup>
10641067
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
10651068
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/App/NLog.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<targets>
5+
<target name="file" xsi:type="File" fileName="${gdc:LogPath}applog.${shortdate}.log" layout="${date}|${level}|${message}|${exception:format=tostring}" concurrentWrites="false" />
6+
</targets>
7+
<rules>
8+
<logger name="*" minlevel="Debug" writeTo="file" />
9+
</rules>
10+
</nlog>

src/Controller/Controller.Uwp/BiliController.Account.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System;
44
using System.Collections.Generic;
5-
using System.Diagnostics;
65
using System.Linq;
76
using System.Threading.Tasks;
87
using Bilibili.App.Interfaces.V1;
@@ -44,8 +43,6 @@ public async Task RequestMyProfileAsync()
4443
{
4544
var profile = await _accountProvider.GetMyInformationAsync();
4645
this.MyInfo = profile;
47-
_loggerModule.LogError(new Exception("测试一下"));
48-
_loggerModule.LogError(new Exception("测试一下2"), true);
4946
}
5047
catch (Exception ex)
5148
{

src/Controller/Controller.Uwp/BiliController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ private void RegisterEvents()
276276
private void RegisterToolkitServices()
277277
{
278278
var serviceCollection = new ServiceCollection()
279-
.AddLogging()
280279
.AddSingleton<IAppToolkit, AppToolkit>()
281280
.AddSingleton<IFileToolkit, FileToolkit>()
282281
.AddSingleton<IResourceToolkit, ResourceToolkit>()

src/Controller/Controller.Uwp/Controller.Uwp.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@
3939
<Content Include="Properties\Controller.Uwp.rd.xml" />
4040
</ItemGroup>
4141
<ItemGroup>
42-
<PackageReference Include="Microsoft.Extensions.Logging">
43-
<Version>5.0.0</Version>
44-
</PackageReference>
4542
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
4643
<Version>6.2.12</Version>
4744
</PackageReference>
48-
<PackageReference Include="Serilog.Extensions.Logging.File">
49-
<Version>2.0.0</Version>
45+
<PackageReference Include="NLog">
46+
<Version>4.7.11</Version>
5047
</PackageReference>
5148
<PackageReference Include="Websocket.Client">
5249
<Version>4.3.36</Version>

src/Controller/Controller.Uwp/Interfaces/ILoggerModule.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Richasy. All rights reserved.
22

33
using System;
4-
using Microsoft.Extensions.Logging;
54

65
namespace Richasy.Bili.Controller.Uwp.Interfaces
76
{
@@ -10,11 +9,6 @@ namespace Richasy.Bili.Controller.Uwp.Interfaces
109
/// </summary>
1110
public interface ILoggerModule
1211
{
13-
/// <summary>
14-
/// 日志记录工厂.
15-
/// </summary>
16-
ILoggerFactory LoggerFactory { get; }
17-
1812
/// <summary>
1913
/// 记录信息.
2014
/// </summary>

src/Controller/Controller.Uwp/Modules/LoggerModule.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Globalization;
55
using System.Linq;
66
using System.Text;
7-
using Microsoft.Extensions.Logging;
87
using Richasy.Bili.Controller.Uwp.Interfaces;
98
using Richasy.Bili.Models.App.Constants;
109
using Windows.ApplicationModel;
@@ -25,26 +24,21 @@ public class LoggerModule : ILoggerModule
2524
private string _deviceFamily;
2625
private string _architecture;
2726

28-
private ILogger _logger;
27+
private NLog.Logger _logger;
2928

3029
/// <summary>
3130
/// Initializes a new instance of the <see cref="LoggerModule"/> class.
3231
/// </summary>
33-
/// <param name="factory">日志构造工厂.</param>
34-
public LoggerModule(ILoggerFactory factory)
32+
public LoggerModule()
3533
{
36-
LoggerFactory = factory;
3734
Initialize();
3835
}
3936

40-
/// <inheritdoc/>
41-
public ILoggerFactory LoggerFactory { get; private set; }
42-
4337
/// <inheritdoc/>
4438
public void LogInformation(string message)
4539
{
4640
var header = GetHeader();
47-
_logger.LogInformation(header + message + "\n");
41+
_logger.Log(NLog.LogLevel.Info, header + message + "\n");
4842
}
4943

5044
/// <inheritdoc/>
@@ -53,12 +47,11 @@ public void LogError(Exception ex, bool isWarning = false)
5347
var header = GetHeader();
5448
if (isWarning)
5549
{
56-
_logger.LogWarning(header + ex.Message + "\n");
57-
_logger.LogTrace(ex, "Warning 堆栈");
50+
_logger.Log(NLog.LogLevel.Warn, ex, header + ex.Message);
5851
}
5952
else
6053
{
61-
_logger.LogError(ex, header);
54+
_logger.Log(NLog.LogLevel.Error, ex, header);
6255
}
6356
}
6457

@@ -81,11 +74,9 @@ private void Initialize()
8174

8275
var rootFolder = ApplicationData.Current.LocalFolder;
8376
var logFolderName = ControllerConstants.Names.LoggerFolder;
84-
var logFileName = ControllerConstants.Names.LoggerName;
85-
var fullPath = $"{rootFolder.Path}\\{logFolderName}\\{logFileName}";
86-
87-
LoggerFactory.AddFile(fullPath);
88-
_logger = LoggerFactory.CreateLogger("Richasy.Bili");
77+
var fullPath = $"{rootFolder.Path}\\{logFolderName}\\";
78+
NLog.GlobalDiagnosticsContext.Set("LogPath", fullPath);
79+
_logger = NLog.LogManager.GetLogger("Richasy.Bili");
8980
}
9081

9182
private string GetHeader()

0 commit comments

Comments
 (0)