Skip to content

Commit 7ca2626

Browse files
committed
test(ConsoleApp): update Logger
1 parent bd44250 commit 7ca2626

File tree

7 files changed

+133
-87
lines changed

7 files changed

+133
-87
lines changed

PollinationSDK.sln

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.30225.117
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.10.34916.146
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PollinationSDK", "src\PollinationSDK\PollinationSDK.csproj", "{B5D437C0-A194-443B-A9A1-A94594F3D150}"
66
EndProject
@@ -11,21 +11,35 @@ EndProject
1111
Global
1212
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1313
Debug|Any CPU = Debug|Any CPU
14+
Debug|x64 = Debug|x64
1415
Release|Any CPU = Release|Any CPU
16+
Release|x64 = Release|x64
1517
EndGlobalSection
1618
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1719
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1820
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Debug|x64.ActiveCfg = Debug|x64
22+
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Debug|x64.Build.0 = Debug|x64
1923
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Release|Any CPU.ActiveCfg = Release|Any CPU
2024
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Release|x64.ActiveCfg = Release|x64
26+
{B5D437C0-A194-443B-A9A1-A94594F3D150}.Release|x64.Build.0 = Release|x64
2127
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2228
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Debug|x64.ActiveCfg = Debug|x64
30+
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Debug|x64.Build.0 = Debug|x64
2331
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Release|Any CPU.ActiveCfg = Release|Any CPU
2432
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Release|x64.ActiveCfg = Release|x64
34+
{9B368D0E-9965-4FFF-A2EF-1C828558C960}.Release|x64.Build.0 = Release|x64
2535
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2636
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Debug|x64.ActiveCfg = Debug|x64
38+
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Debug|x64.Build.0 = Debug|x64
2739
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2840
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Release|x64.ActiveCfg = Release|x64
42+
{397E3A60-40B0-4718-B3D6-44566E49DB6A}.Release|x64.Build.0 = Release|x64
2943
EndGlobalSection
3044
GlobalSection(SolutionProperties) = preSolution
3145
HideSolutionNode = FALSE

src/ConsoleAppDemo/ConsoleAppDemo.csproj

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7</TargetFramework>
5+
<TargetFramework>net8</TargetFramework>
66
<StartupObject>ConsoleAppDemo.Program</StartupObject>
7+
<Platforms>AnyCPU;x64</Platforms>
78
</PropertyGroup>
89
<PropertyGroup Condition="'$(Configuration)'=='Release'">
910
<DebugType>none</DebugType>
1011
<DebugSymbols>false</DebugSymbols>
1112
</PropertyGroup>
1213

13-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
14-
</PropertyGroup>
15-
14+
<ItemGroup>
15+
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
16+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
17+
</ItemGroup>
1618
<ItemGroup>
1719
<ProjectReference Include="..\PollinationSDK\PollinationSDK.csproj" />
1820
</ItemGroup>

src/ConsoleAppDemo/LogHelper.cs

-67
This file was deleted.

src/ConsoleAppDemo/LogSetup.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Serilog.Extensions.Logging;
2+
using Serilog;
3+
using System.IO;
4+
using Serilog.Core;
5+
using Serilog.Events;
6+
using System.Linq;
7+
using System;
8+
9+
namespace ConsoleAppDemo
10+
{
11+
public static class LogSetup
12+
{
13+
private static string _logFilePath;
14+
private static LoggingLevelSwitch _levelSwitch = new LoggingLevelSwitch(LogEventLevel.Information); // Initial level
15+
16+
public static string LogFolder
17+
{
18+
get
19+
{
20+
var userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
21+
var podir = Path.Combine(userAppData, "pollination", "log");
22+
if (!Directory.Exists(podir))
23+
Directory.CreateDirectory(podir);
24+
return podir;
25+
}
26+
}
27+
28+
public static void OneTimeSetup(string logFileName = default)
29+
{
30+
var file = string.IsNullOrEmpty(logFileName) ? "log.txt" : logFileName;
31+
32+
_logFilePath = Path.Combine(LogFolder, file);
33+
34+
Serilog.Log.Logger = new Serilog.LoggerConfiguration()
35+
.MinimumLevel.ControlledBy(_levelSwitch)
36+
//.Enrich.FromLogContext()
37+
//.Enrich.With<CallingContextEnricher>()
38+
.WriteTo.File(
39+
_logFilePath,
40+
rollingInterval: RollingInterval.Hour,
41+
rollOnFileSizeLimit: true,
42+
retainedFileCountLimit: 5,
43+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties:j}{NewLine}"
44+
).CreateLogger();
45+
46+
var loggerFactory = new SerilogLoggerFactory(Serilog.Log.Logger);
47+
Pollination.LogHelper.OneTimeSetup(loggerFactory);
48+
}
49+
50+
public static string GetTheLatestLog()
51+
{
52+
var folder = new DirectoryInfo(LogFolder);
53+
if (!folder.Exists) return "";
54+
55+
var lastLog = folder.GetFiles().OrderBy(_ => _.LastWriteTime).LastOrDefault();
56+
if (lastLog != null && lastLog.Exists) return lastLog.FullName;
57+
else return "";
58+
}
59+
60+
public static void SetLogLevel(LogEventLevel level)
61+
{
62+
_levelSwitch.MinimumLevel = level;
63+
}
64+
65+
public static void Dispose()
66+
{
67+
if (!string.IsNullOrEmpty(_logFilePath))
68+
{
69+
Log.CloseAndFlush();
70+
}
71+
72+
}
73+
}
74+
}

src/ConsoleAppDemo/LogUtils.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
4+
namespace ConsoleAppDemo
5+
{
6+
internal static class LogUtils
7+
{
8+
private static string _namespaceName = System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? "Unknown Namespace";
9+
private static string _className = System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType?.Name ?? "Unknown Class";
10+
private static string _logCatagory = $"{_namespaceName}.{_className}";
11+
12+
private static Pollination.LogHelper? _helper;
13+
internal static Pollination.LogHelper Helper => _helper ??= new Pollination.LogHelper(_logCatagory);
14+
15+
internal static ILogger GetLogger<T>() => Helper.GetLogger<T>();
16+
internal static ILogger GetLogger(Type type) => Helper.GetLogger(type);
17+
internal static ILogger GetLogger(string catName) => Helper.GetLogger(catName);
18+
19+
internal static ILogger Logger => GetLogger(_logCatagory);
20+
}
21+
22+
}

src/ConsoleAppDemo/Program.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
using PollinationSDK;
99
using PollinationSDK.Wrapper;
1010
using System.Threading;
11-
using Serilog;
11+
using Pollination;
1212

1313
namespace ConsoleAppDemo
1414
{
1515
class Program
1616
{
1717

18+
private static Microsoft.Extensions.Logging.ILogger Logger => LogUtils.GetLogger<Program>();
1819
static void Main(string[] args)
1920
{
2021

@@ -158,11 +159,10 @@ public static void GetJobHttpInfo(string jobString)
158159
{
159160
if (string.IsNullOrEmpty(jobString))
160161
throw new ArgumentException("Invalid Job ID");
161-
LogHelper.SetupLogger("logs.DemoApp.txt");
162-
Helper.Logger = LogHelper.Log;
162+
LogSetup.OneTimeSetup("logs.DemoApp.txt");
163163

164164
var me = Helper.CurrentUser;
165-
PollinationSDK.Helper.Logger.Information($"You are: {me.Username}");
165+
Logger.Info($"You are: {me.Username}");
166166

167167
var texts = jobString.Split('/').Select(_ => _.Trim()).Where(_ => !string.IsNullOrEmpty(_)).ToList();
168168

@@ -172,37 +172,37 @@ public static void GetJobHttpInfo(string jobString)
172172
Console.WriteLine($"=> Project Owner: {projOwner}");
173173
Console.WriteLine($"=> Project Name: {projName}");
174174
Console.WriteLine($"=> Job ID: {jobID}");
175-
Console.WriteLine($"Log path: {LogHelper.GetTheLatestLog()}");
175+
Console.WriteLine($"Log path: {LogSetup.GetTheLatestLog()}");
176176

177-
PollinationSDK.Helper.Logger.Information($"DemoApp: getting project [{projOwner}/{projName}].");
177+
Logger.Info($"DemoApp: getting project [{projOwner}/{projName}].");
178178
var projApi = new PollinationSDK.Api.ProjectsApi();
179179
var proj = projApi.GetProject(projOwner, projName);
180-
PollinationSDK.Helper.Logger.Information($"DemoApp: got the project [{proj.Owner.Name}/{proj.Name}].");
181-
PollinationSDK.Helper.Logger.Information($"DemoApp: Got the Project: {Environment.NewLine}{proj.ToJson()}");
180+
Logger.Info($"DemoApp: got the project [{proj.Owner.Name}/{proj.Name}].");
181+
Logger.Info($"DemoApp: Got the Project: {Environment.NewLine}{proj.ToJson()}");
182182
Console.WriteLine($"DemoApp: got the project [{proj.Owner.Name}/{proj.Name}].");
183183

184-
PollinationSDK.Helper.Logger.Information($"DemoApp: getting job [{proj.Owner.Name}/{proj.Name}/{jobID}].");
184+
Logger.Info($"DemoApp: getting job [{proj.Owner.Name}/{proj.Name}/{jobID}].");
185185
var api = new PollinationSDK.Api.JobsApi();
186186
var jobHttp = api.GetJobWithHttpInfo(proj.Owner.Name, proj.Name, jobID.ToString());
187-
PollinationSDK.Helper.Logger.Information($"DemoApp: got job with HTTP info:");
187+
Logger.Info($"DemoApp: got job with HTTP info:");
188188

189-
PollinationSDK.Helper.Logger.Information($"API Response StatusCode: {jobHttp?.StatusCode}.");
189+
Logger.Info($"API Response StatusCode: {jobHttp?.StatusCode}.");
190190
Console.WriteLine($"API Response StatusCode: {jobHttp?.StatusCode}.");
191191
var headers = jobHttp.Headers.Select(_ => $"{_.Key}:{_.Value}");
192192
foreach (var item in headers)
193193
{
194194
var msg = $"API Response Header: {item}.";
195-
PollinationSDK.Helper.Logger.Information(msg);
195+
Logger.Info(msg);
196196
Console.WriteLine(msg);
197197
}
198198

199-
PollinationSDK.Helper.Logger.Information($"API Response Data: {jobHttp?.Data?.ToJson()}.");
199+
Logger.Info($"API Response Data: {jobHttp?.Data?.ToJson()}.");
200200
Console.WriteLine($"API Response Data: {jobHttp?.Data?.ToJson()}.");
201201

202202
}
203203
catch (Exception e)
204204
{
205-
PollinationSDK.Helper.Logger.Error(e, $"DemoApp");
205+
Logger.Error(e);
206206
Console.WriteLine($"Check the log file for the error.");
207207
}
208208

src/PollinationSDK.Tests/PollinationSDK.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net8</TargetFramework>
55
<IsPackable>false</IsPackable>
6+
<Platforms>AnyCPU;x64</Platforms>
67
</PropertyGroup>
78

89
<ItemGroup>

0 commit comments

Comments
 (0)