Skip to content

Commit

Permalink
introduce ModuleLoggerAspectAttribute & ServiceLoggerAspectAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Jul 12, 2021
1 parent 6f40c1b commit 014a36c
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace Solti.Utils.Rpc.Interfaces
/// Marks a module or service to be logged.
/// </summary>
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public sealed class LoggerAspectAttribute: AspectAttribute
[SuppressMessage("Performance", "CA1813:Avoid unsealed attributes")]
public class LoggerAspectAttribute: AspectAttribute
{
/// <summary>
/// Creates a new <see cref="LoggerAspectAttribute"/> instance.
Expand All @@ -30,13 +31,7 @@ public sealed class LoggerAspectAttribute: AspectAttribute
/// <summary>
/// Creates a new <see cref="LoggerAspectAttribute"/> instance.
/// </summary>
public LoggerAspectAttribute() => DefaultLoggers = new LoggerBase[]
{
new ModuleMethodScopeLogger(),
new ExceptionLogger(),
new ParameterLogger(),
new StopWatchLogger()
};
public LoggerAspectAttribute(): this(Array.Empty<Type>()) { }

/// <summary>
/// The default loggers. This value can be overridden per methods by the <see cref="LoggersAttribute"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/********************************************************************************
* ModuleLoggerAspectAttribute.cs *
* *
* Author: Denes Solti *
********************************************************************************/
using System;

namespace Solti.Utils.Rpc.Interfaces
{
/// <summary>
/// Marks a module to be logged.
/// </summary>
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public sealed class ModuleLoggerAspectAttribute: LoggerAspectAttribute
{
/// <summary>
/// Creates a new <see cref="ModuleLoggerAspectAttribute"/> instance.
/// </summary>
public ModuleLoggerAspectAttribute() : base(typeof(ModuleMethodScopeLogger), typeof(ExceptionLogger), typeof(ParameterLogger), typeof(StopWatchLogger)) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/********************************************************************************
* ServiceLoggerAspectAttribute.cs *
* *
* Author: Denes Solti *
********************************************************************************/
using System;

namespace Solti.Utils.Rpc.Interfaces
{
/// <summary>
/// Marks a service to be logged.
/// </summary>
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public sealed class ServiceLoggerAspectAttribute: LoggerAspectAttribute
{
/// <summary>
/// Creates a new <see cref="ServiceLoggerAspectAttribute"/> instance.
/// </summary>
public ServiceLoggerAspectAttribute() : base(typeof(ServiceMethodScopeLogger), typeof(ExceptionLogger), typeof(ParameterLogger)) { }
}
}
2 changes: 1 addition & 1 deletion SRC/RPC.Interfaces/Loggers/ExceptionLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* LoggerBase.cs *
* ExceptionLogger.cs *
* *
* Author: Denes Solti *
********************************************************************************/
Expand Down
5 changes: 3 additions & 2 deletions SRC/RPC.Interfaces/Loggers/ModuleMethodScopeLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* LoggerBase.cs *
* ModuleMethodScopeLogger.cs *
* *
* Author: Denes Solti *
********************************************************************************/
Expand All @@ -14,7 +14,7 @@ namespace Solti.Utils.Rpc.Interfaces
/// Creates a new log scope containing the module and method name and the session id.
/// </summary>
/// <remarks>This logger is intended to be used on modules only.</remarks>
public sealed class ModuleMethodScopeLogger : LoggerBase
public sealed class ModuleMethodScopeLogger: LoggerBase
{
/// <inheritdoc/>
public override object? Invoke(LogContext context, Func<object?> callNext)
Expand All @@ -26,6 +26,7 @@ public sealed class ModuleMethodScopeLogger : LoggerBase
[nameof(cntx.Module)] = cntx.Module,
[nameof(cntx.Method)] = cntx.Method,
[nameof(cntx.SessionId)] = cntx.SessionId ?? "NULL"
// TODO: modul nev
});

return callNext();
Expand Down
2 changes: 1 addition & 1 deletion SRC/RPC.Interfaces/Loggers/ParameterLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* LoggerBase.cs *
* ParameterLogger.cs *
* *
* Author: Denes Solti *
********************************************************************************/
Expand Down
29 changes: 29 additions & 0 deletions SRC/RPC.Interfaces/Loggers/ServiceMethodScopeLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********************************************************************************
* ServiceMethodScopeLogger.cs *
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Collections.Generic;

namespace Solti.Utils.Rpc.Interfaces
{
/// <summary>
/// Creates a new log scope containing the service and method name.
/// </summary>
public sealed class ServiceMethodScopeLogger: LoggerBase
{
/// <inheritdoc/>
public override object? Invoke(LogContext context, Func<object?> callNext)
{
using IDisposable? logScope = context.Logger.BeginScope(new Dictionary<string, object>
{
["Service"] = context.Method.ReflectedType,
["Method"] = context.Method
// TODO: szerviz nev
});

return callNext();
}
}
}
2 changes: 1 addition & 1 deletion SRC/RPC.Interfaces/Loggers/StopWatchLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* LoggerBase.cs *
* StopWatchLogger.cs *
* *
* Author: Denes Solti *
********************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion TEST/RPC.Server.Sample.Interfaces/ICalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Solti.Utils.Rpc.Server.Sample.Interfaces
{
using Rpc.Interfaces;

[LoggerAspect]
[ModuleLoggerAspect]
[ParameterValidatorAspect]
public interface ICalculator
{
Expand Down
2 changes: 1 addition & 1 deletion TEST/RPC.Tests/Aspects/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Solti.Utils.Rpc.Aspects.Tests
[TestFixture]
public class LoggerTests
{
[LoggerAspect]
[ModuleLoggerAspect]
public interface IModule
{
void DoSomething(string arg1, object arg2);
Expand Down

0 comments on commit 014a36c

Please sign in to comment.