Skip to content

Commit

Permalink
Properly filtering calling assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Mols committed Aug 14, 2023
1 parent eb26e67 commit e87276d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A simple Serilog enricher to add information about the calling method. Loosely b

Performance may be low due the usage of reflection to obtain assembly and method information, as well as the retrieval of the current stacktrace whenever a new log event is added.

To get the actual method that emitted the log event, the stack trace is traversed and the first method that is in a matching assembly is considered the correct one. To configure which assemblies are the correct one, one can either use the configuration method with `assemblyPrefix` or the one that directly accepts a collection of fully qualified assembly names. When passing in a prefix, the assembly that calls the configuration method is recursively analyzed for referenced assemblies and whether they match the prefix. If you e.g. have a solution with projects being called `MySolution.Web`, `MySolution.Domain`, etc., you can limit the included assemblies by passing in the prefix `MySolution`.
To get the actual method that emitted the log event, the stack trace is traversed and the first method that is in a matching assembly is considered the correct one. To configure which assemblies are the correct one, one can either use the configuration method with `assemblyPrefix` or the one that directly accepts a collection of fully qualified assembly names. When passing in a prefix, the assembly that calls the configuration method is recursively analyzed for referenced assemblies and whether they match the prefix. If you e.g. have a solution with projects being called `MySolution.Web`, `MySolution.Domain`, etc., you can limit the included assemblies by passing in the prefix `MySolution`. Be aware that the assembly from which you configure the logger must also have the correct prefix, otherwise no assemblies will be considered and the enricher won't work.

## Usage

Expand Down
7 changes: 3 additions & 4 deletions Serilog.Enrichers.CallerInfo/EnricherConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ private static IEnumerable<string> GetAssemblies(Assembly start, Func<AssemblyNa
{
var asmNames = new List<string>();
var stack = new Stack<Assembly>();

stack.Push(start);

do
{
var asm = stack.Pop();

if (filter(asm.GetName()))
if (!filter(asm.GetName()))
{
asmNames.Add(asm.GetName().Name);
continue;
}

asmNames.Add(asm.GetName().Name);
foreach (var reference in asm.GetReferencedAssemblies())
{
if (!filter(reference))
Expand Down

0 comments on commit e87276d

Please sign in to comment.