This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete first version of a basic logger.
- Loading branch information
silentdevnull
committed
Dec 27, 2021
1 parent
0ad8b1c
commit fff4233
Showing
17 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": {} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30114.105 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{82ED88E6-BE10-406F-92E6-EB0348817FD3}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SilentDevNull.SmplLogger", "src\SilentDevNull.SmplLogger\SilentDevNull.SmplLogger.csproj", "{B399EA71-364D-43CD-A684-297ACE024767}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmplLoggerConsoleTest", "src\SmplLoggerConsoleTest\SmplLoggerConsoleTest.csproj", "{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B399EA71-364D-43CD-A684-297ACE024767}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B399EA71-364D-43CD-A684-297ACE024767}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B399EA71-364D-43CD-A684-297ACE024767}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B399EA71-364D-43CD-A684-297ACE024767}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{B399EA71-364D-43CD-A684-297ACE024767} = {82ED88E6-BE10-406F-92E6-EB0348817FD3} | ||
{3C78C3E6-8D1F-4AFB-B1FB-85F937B28B42} = {82ED88E6-BE10-406F-92E6-EB0348817FD3} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "6.0.101" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
global using System; | ||
global using System.Collections.Concurrent; | ||
global using System.Collections.Generic; | ||
global using System.IO; | ||
global using System.Text; | ||
global using System.Text.Json; | ||
|
||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.DependencyInjection.Extensions; | ||
global using Microsoft.Extensions.Logging; | ||
global using Microsoft.Extensions.Logging.Configuration; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace SilentDevNull.SmplLogger; | ||
public class LogFileWriter | ||
{ | ||
public LogFileWriter() | ||
{ | ||
} | ||
|
||
public void Log(String logMsg) | ||
{ | ||
Console.WriteLine($"Log message: \n\t {logMsg}"); | ||
string fileName = string.Format($"log-{DateTime.Now:yyyy-MM-dd}.log"); | ||
|
||
//using (StreamWriter wr = new StreamWriter(LogName, true)) | ||
using StreamWriter wr = new StreamWriter(fileName, true); | ||
wr.WriteLine(logMsg); | ||
wr.Close(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/SilentDevNull.SmplLogger/SilentDevNull.SmplLogger.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<FileVersion>1.0.0</FileVersion> | ||
<Version>1.0.0</Version> | ||
<PublishSingleFile>true</PublishSingleFile> | ||
<SelfContained>true</SelfContained> | ||
<PublishReadyToRun>true</PublishReadyToRun> | ||
<Authors>SilentDevNull</Authors> | ||
<Company> </Company> | ||
<Description>Basic logger to create log file. This is using ILogger</Description> | ||
<RepositoryUrl>https://github.com/silentdevnull/SmplLogger</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/silentdevnull/SmplLogger</PackageProjectUrl> | ||
<PackageTags>Logger</PackageTags> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace SilentDevNull.SmplLogger; | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class SmplLogger : ILogger | ||
{ | ||
private readonly string _name = String.Empty; | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public SmplLogger() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="name"></param> | ||
public SmplLogger(string name) | ||
{ | ||
_name = name; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="state"></param> | ||
/// <typeparam name="TState"></typeparam> | ||
/// <returns></returns> | ||
public IDisposable BeginScope<TState>(TState state) => default!; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="logLevel"></param> | ||
/// <returns></returns> | ||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="logLevel"></param> | ||
/// <param name="eventId"></param> | ||
/// <param name="state"></param> | ||
/// <param name="exception"></param> | ||
/// <param name="formatter"></param> | ||
/// <typeparam name="TState"></typeparam> | ||
public void Log<TState>( | ||
LogLevel logLevel, | ||
EventId eventId, | ||
TState state, | ||
Exception? exception, | ||
Func<TState, Exception, string> formatter) | ||
{ | ||
#if(DEBUG) | ||
Console.WriteLine(_name); | ||
Console.WriteLine($"LogLevel: {logLevel} EventId: {eventId} State: {state} exception: {exception}"); | ||
#endif | ||
var log = string.Format($"{logLevel}\t{eventId}\t{state}\t{exception}"); | ||
Console.WriteLine(log); | ||
LogFileWriter logFileWriter = new LogFileWriter(); | ||
logFileWriter.Log(log); | ||
//logFileWriter.Log(logLevel,eventId,state,exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace SilentDevNull.SmplLogger; | ||
|
||
public static class SmplLoggerExtensions | ||
{ | ||
public static ILoggingBuilder AddSmplLogger(this ILoggingBuilder builder) | ||
{ | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider,SmplLoggerProvider>()); | ||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace SilentDevNull.SmplLogger; | ||
|
||
public class SmplLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly ConcurrentDictionary<string, SmplLogger> _loggers = new(); | ||
|
||
public SmplLoggerProvider() | ||
{ | ||
} | ||
|
||
public SmplLoggerProvider(EventHandler onCreateLogger) | ||
{ | ||
OnCreateLogger = onCreateLogger; | ||
} | ||
public event EventHandler OnCreateLogger = delegate { }; | ||
|
||
public ILogger CreateLogger(string categoryName) => | ||
_loggers.GetOrAdd(categoryName, name => new SmplLogger(name)); | ||
|
||
public void Dispose() | ||
{ | ||
_loggers.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using SilentDevNull.SmplLogger; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using ILoggerFactory loggerFactory = | ||
LoggerFactory.Create(builder => | ||
builder.AddSmplLogger()); | ||
ILogger<Program> logger = loggerFactory.CreateLogger<Program>(); | ||
|
||
logger.LogInformation("Test"); | ||
logger.LogDebug("test debug"); | ||
logger.LogError("Error"); | ||
logger.LogInformation("Test1"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SilentDevNull.SmplLogger\SilentDevNull.SmplLogger.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" /> | ||
</ItemGroup> | ||
</Project> |