Reputation: 1561
Want to use PostSharp diagnostics with an aspect for exception handling encapsulated in a library assembly.
At the same time trying to setup and initialize a Serilog logger in the consumer assembly of that diagnostics library.
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.After,
typeof(AddContextOnExceptionAttribute))]
[PSerializable]
public sealed class ReportAndSwallowExceptionAttribute : OnExceptionAspect
{
public ILogger TheLogger { get; set; }
public ReportAndSwallowExceptionAttribute(ILogger logger)
{
TheLogger = logger;
}
public override void OnException(MethodExecutionArgs args)
{
TheLogger.Error("Error happened ...");
}
}
In the main class:
class Program
{
// below line is the critical part which seems ILogger is not allowed
[ReportAndSwallowException(Log.Logger)]
public static void TryExceptionMethod()
{
throw new NotImplementedException();
}
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File(@"HERE\Logs\log-x.log",
rollingInterval: RollingInterval.Day)
.CreateLogger();
Console.WriteLine("Hello World!");
TryExceptionMethod();
}
}
Seems passing ILogger
to that attribute is illegal, how can I achieve this scenario?
Current Error:
Error CS0181: Attribute constructor parameter 'logger' has type 'ILogger', which is not a valid attribute parameter type
Think this error needs a constant to be solved, but the main question is how to achieve this scenario: have the logger in the consumer proj, have the aspects in a library.
Upvotes: 1
Views: 241
Reputation: 6857
The easiest option here is that your aspect directly references Log.Logger.
There are several other more complex options that are documented here: https://doc.postsharp.net/consuming-dependencies
Upvotes: 2