Skip to content

Commit 5ec842b

Browse files
committed
NLog
添加 Nlog
1 parent 238a68f commit 5ec842b

File tree

7 files changed

+241
-1
lines changed

7 files changed

+241
-1
lines changed

AA.FrameWork.Tests.Unit/AA.FrameWork.Tests.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<ProjectReference Include="..\AA.Dapper\AA.Dapper.csproj" />
2020
<ProjectReference Include="..\AA.FrameWork\AA.FrameWork.csproj" />
2121
<ProjectReference Include="..\AA.Log4Net\AA.Log4Net.csproj" />
22+
<ProjectReference Include="..\AA.NLog\AA.NLog.csproj" />
2223
<ProjectReference Include="..\AA.RabbitMQ\AA.RabbitMQ.csproj" />
2324
<ProjectReference Include="..\AA.Redis\AA.Redis.csproj" />
2425
<ProjectReference Include="..\AA.ServiceBus\AA.ServiceBus.csproj" />
@@ -32,6 +33,9 @@
3233
<None Update="Log4Net\log4net.config">
3334
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3435
</None>
36+
<None Update="NLog\nlog.config">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
3539
</ItemGroup>
3640

3741
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using AA.FrameWork.Logging;
2+
using AA.NLogging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace AA.FrameWork.Tests.Unit.NLog
9+
{
10+
public class NLogTest
11+
{
12+
[Fact]
13+
public void TestNLog()
14+
{
15+
NLogLogger.Use("NLog/nlog.config");
16+
ILog log = Logger.Get(typeof(NLogTest));
17+
log.Debug("test nlog record");
18+
}
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
autoReload="true"
5+
internalLogLevel="Trace"
6+
internalLogFile="c:\temp\internallog.txt">
7+
8+
<targets>
9+
<target name="logfile" xsi:type="File"
10+
fileName="${basedir}/logs/${shortdate}_logfile.txt"
11+
layout="${longdate} ${level:uppercase=true} ${message}"/>
12+
</targets>
13+
14+
<rules>
15+
<logger name="*" minlevel="Debug" writeTo="logfile" />
16+
</rules>
17+
</nlog>

AA.NLog/AA.NLog.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
8+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
9+
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="NLog" Version="4.6.8" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\AA.FrameWork\AA.FrameWork.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

AA.NLog/NLogLog.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using AA.FrameWork.Logging;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace AA.NLogging
7+
{
8+
public class NLogLog : ILog
9+
{
10+
11+
private readonly NLog.Logger _log;
12+
13+
public NLogLog(NLog.Logger log)
14+
{
15+
_log = log;
16+
}
17+
18+
public void Debug(object obj)
19+
{
20+
_log.Log(NLog.LogLevel.Debug, obj);
21+
}
22+
23+
public void Debug(object obj, Exception exception)
24+
{
25+
_log.Log(NLog.LogLevel.Debug, exception, obj?.ToString() ?? "");
26+
}
27+
28+
29+
public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args)
30+
{
31+
_log.Log(NLog.LogLevel.Debug, formatProvider, format, args);
32+
}
33+
34+
public void DebugFormat(string format, params object[] args)
35+
{
36+
_log.Log(NLog.LogLevel.Debug, format, args);
37+
}
38+
39+
public void Error(object obj)
40+
{
41+
_log.Log(NLog.LogLevel.Error, obj);
42+
}
43+
44+
public void Error(object obj, Exception exception)
45+
{
46+
_log.Log(NLog.LogLevel.Error, exception, obj?.ToString() ?? "");
47+
}
48+
49+
public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args)
50+
{
51+
_log.Log(NLog.LogLevel.Error, formatProvider, format, args);
52+
}
53+
54+
public void ErrorFormat(string format, params object[] args)
55+
{
56+
_log.Log(NLog.LogLevel.Error, format, args);
57+
}
58+
59+
public void Fatal(object obj)
60+
{
61+
_log.Log(NLog.LogLevel.Fatal, obj);
62+
}
63+
64+
public void Fatal(object obj, Exception exception)
65+
{
66+
_log.Log(NLog.LogLevel.Fatal, exception, obj?.ToString() ?? "");
67+
}
68+
69+
public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args)
70+
{
71+
_log.Log(NLog.LogLevel.Fatal, formatProvider, format, args);
72+
}
73+
74+
public void FatalFormat(string format, params object[] args)
75+
{
76+
_log.Log(NLog.LogLevel.Fatal, format, args);
77+
}
78+
79+
public void Info(object obj)
80+
{
81+
_log.Log(NLog.LogLevel.Info, obj);
82+
}
83+
84+
public void Info(object obj, Exception exception)
85+
{
86+
_log.Log(NLog.LogLevel.Info, exception, obj?.ToString() ?? "");
87+
}
88+
89+
public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args)
90+
{
91+
_log.Log(NLog.LogLevel.Warn, formatProvider, format, args);
92+
}
93+
94+
public void InfoFormat(string format, params object[] args)
95+
{
96+
_log.Log(NLog.LogLevel.Info, format, args);
97+
}
98+
99+
public void Warn(object obj)
100+
{
101+
_log.Log(NLog.LogLevel.Warn, obj);
102+
}
103+
104+
public void Warn(object obj, Exception exception)
105+
{
106+
_log.Log(NLog.LogLevel.Warn, exception, obj?.ToString() ?? "");
107+
}
108+
109+
public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args)
110+
{
111+
_log.Log(NLog.LogLevel.Warn, formatProvider, format, args);
112+
}
113+
114+
public void WarnFormat(string format, params object[] args)
115+
{
116+
_log.Log(NLog.LogLevel.Warn, format, args);
117+
}
118+
119+
public bool IsDebugEnabled => _log.IsDebugEnabled;
120+
121+
public bool IsInfoEnabled => _log.IsInfoEnabled;
122+
123+
public bool IsWarnEnabled => _log.IsWarnEnabled;
124+
125+
public bool IsErrorEnabled => _log.IsErrorEnabled;
126+
127+
public bool IsFatalEnabled => _log.IsFatalEnabled;
128+
}
129+
}

AA.NLog/NLogLogger.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using AA.FrameWork.Logging;
2+
using NLog;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
8+
namespace AA.NLogging
9+
{
10+
public class NLogLogger : FrameWork.Logging.ILogger
11+
{
12+
private readonly bool _shutdownLogManager;
13+
14+
public NLogLogger()
15+
{
16+
_shutdownLogManager = true;
17+
}
18+
public ILog Get(string name)
19+
{
20+
var logger = LogManager.GetLogger(name);
21+
return new NLogLog(logger);
22+
}
23+
24+
public void Shutdown()
25+
{
26+
LogManager.Flush();
27+
28+
if (_shutdownLogManager)
29+
LogManager.Shutdown();
30+
}
31+
32+
public static void Use(string file)
33+
{
34+
FrameWork.Logging.Logger.UseLogger(new NLogLogger());
35+
#if NETCORE
36+
file = Path.Combine(AppContext.BaseDirectory, file);
37+
#else
38+
file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
39+
#endif
40+
LogManager.LoadConfiguration(file);
41+
}
42+
}
43+
}

AA.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bus", "Bus", "{1A7C5724-94D
3131
EndProject
3232
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AA.RabbitMQ", "AA.RabbitMQ\AA.RabbitMQ.csproj", "{78AD2006-B557-4800-80B6-D881239C11EB}"
3333
EndProject
34-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AA.ServiceBus", "AA.ServiceBus\AA.ServiceBus.csproj", "{B42A7FA6-9976-447A-9D32-B02A2D5162E5}"
34+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AA.ServiceBus", "AA.ServiceBus\AA.ServiceBus.csproj", "{B42A7FA6-9976-447A-9D32-B02A2D5162E5}"
35+
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AA.NLog", "AA.NLog\AA.NLog.csproj", "{A500ABFE-FF4C-4D53-B595-FE17683EBCC9}"
3537
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -75,6 +77,10 @@ Global
7577
{B42A7FA6-9976-447A-9D32-B02A2D5162E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
7678
{B42A7FA6-9976-447A-9D32-B02A2D5162E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
7779
{B42A7FA6-9976-447A-9D32-B02A2D5162E5}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{A500ABFE-FF4C-4D53-B595-FE17683EBCC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{A500ABFE-FF4C-4D53-B595-FE17683EBCC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{A500ABFE-FF4C-4D53-B595-FE17683EBCC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{A500ABFE-FF4C-4D53-B595-FE17683EBCC9}.Release|Any CPU.Build.0 = Release|Any CPU
7884
EndGlobalSection
7985
GlobalSection(SolutionProperties) = preSolution
8086
HideSolutionNode = FALSE
@@ -86,6 +92,7 @@ Global
8692
{CD808245-157E-4A56-A4A8-773E69DFF7EB} = {AEE6CEF8-1B49-48F5-AC31-86EB03092BD0}
8793
{78AD2006-B557-4800-80B6-D881239C11EB} = {41BB2F48-D50C-4712-A156-C03C23510F96}
8894
{B42A7FA6-9976-447A-9D32-B02A2D5162E5} = {1A7C5724-94D0-4D29-9A20-C22DC3FE42A6}
95+
{A500ABFE-FF4C-4D53-B595-FE17683EBCC9} = {AA75FE19-B13C-4440-AA5A-602375FCC23A}
8996
EndGlobalSection
9097
GlobalSection(ExtensibilityGlobals) = postSolution
9198
SolutionGuid = {537C9B6A-908A-41B2-9C9D-79EB5E29589C}

0 commit comments

Comments
 (0)