Skip to content

Commit

Permalink
Merge pull request #6 from Redacted-Team/vergason-sprint-4
Browse files Browse the repository at this point in the history
Added timestamps classes and updated Program.cs
  • Loading branch information
cpshahan authored Apr 9, 2024
2 parents af750f2 + 1a65061 commit cfbed8a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
41 changes: 41 additions & 0 deletions APIGateway/Logging/TimestampLogger.cs
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
}
}
}
27 changes: 27 additions & 0 deletions APIGateway/Logging/TimestampLoggerExtensions.cs
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;
}
}
}
28 changes: 28 additions & 0 deletions APIGateway/Logging/TimestampLoggerProvider.cs
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() { }
}
}
10 changes: 10 additions & 0 deletions APIGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using APIGateway;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -10,6 +12,14 @@
// Register HttpClient
builder.Services.AddHttpClient();

// This creates the services for the Logging files.
builder.Services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
builder.AddTimestampLogger(); // Add timestamp logger
});

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down

0 comments on commit cfbed8a

Please sign in to comment.