-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Redacted-Team/vergason-sprint-4
Added timestamps classes and updated Program.cs
- Loading branch information
Showing
4 changed files
with
106 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,41 @@ | ||
namespace APIGateway | ||
{ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
|
||
// This class details the beginning steps of the logger with timestamps. It implements ILogger, | ||
// which comes with a few methods to be fulfilled. | ||
|
||
// This was written via ChatGPT 3.5. | ||
public class TimestampLogger : ILogger | ||
{ | ||
// This method is called when a new scope is requested for logging. | ||
// Scopes are used to provide additional contextual information for log messages. | ||
// In this implementation, it simply returns null because no specific scope is being managed. | ||
public IDisposable BeginScope<TState>(TState state) | ||
{ | ||
return null; | ||
} | ||
|
||
// This method is called to check if logging at the specified log level is enabled. | ||
// It can be used to implement filtering based on log levels. | ||
// In this implementation, it always returns true, indicating that logging at any level is enabled. | ||
// You may modify this method to implement filtering based on log levels if needed. | ||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return true; // You may implement filtering based on log level | ||
} | ||
|
||
// This method is called to perform the actual logging of a message. | ||
// It formats the log message with a timestamp in the format "yyyy-MM-dd HH:mm:ss", | ||
// combines it with the formatted state and exception provided by the formatter function, | ||
// and writes the resulting message to the console. | ||
// You can replace Console.WriteLine(message) with your preferred logging mechanism. | ||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
string message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} - {formatter(state, exception)}"; | ||
|
||
Console.WriteLine(message); // Change this to your preferred logging mechanism | ||
} | ||
} | ||
} |
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,27 @@ | ||
namespace APIGateway | ||
{ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using static IServiceCollection; | ||
|
||
// This was written via ChatGPT 3.5. | ||
|
||
public static class TimestampLoggerExtensions | ||
{ | ||
// This extension method is used to extend the functionality of the ILoggingBuilder interface. | ||
// It adds the capability to configure the logging system to include a timestamp in log messages. | ||
// The method adds a timestamp logger to the logging builder. | ||
|
||
public static ILoggingBuilder AddTimestampLogger(this ILoggingBuilder builder) | ||
{ | ||
// Inside the method, it accesses the IServiceCollection provided by the logging builder (builder.Services). | ||
// It registers the TimestampLoggerProvider as a singleton service for ILoggerProvider. | ||
// This ensures that the TimestampLoggerProvider will be used to create logger instances. | ||
builder.Services.AddSingleton<ILoggerProvider, TimestampLoggerProvider>(); | ||
|
||
// Finally, it returns the logging builder instance to support method chaining. | ||
// This allows further configuration to be chained after adding the timestamp logger. | ||
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,28 @@ | ||
namespace APIGateway | ||
{ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
|
||
// This was written via ChatGPT 3.5. | ||
|
||
// This class creates the baseline for the LoggerProvider. It implements ILoggerProvider, | ||
// which simply just needs to create a logger and return it, whilst disposing of any extras. | ||
public class TimestampLoggerProvider : ILoggerProvider | ||
{ | ||
// This method is called when a logger is requested for a specific category. | ||
// It creates and returns a new instance of the TimestampLogger class. | ||
// Each logger provider can be associated with one or more logger instances. | ||
// In this case, it always returns a new instance of TimestampLogger for any requested category. | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
// Creates and returns a new instance of TimestampLogger. | ||
return new TimestampLogger(); | ||
} | ||
|
||
// This method is called to release any resources used by the logger provider. | ||
// Since this implementation does not hold any resources that need to be released explicitly, | ||
// this method doesn't perform any action. | ||
public void Dispose() { } | ||
} | ||
} |
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