Skip to content
An extension for ReSharper that highlights structured logging templates and contains some useful analyzers
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images Add compile time constant analyzer Mar 10, 2019
src Fix contextual logging analyzer Mar 16, 2019
test
.gitattributes Initial commit Mar 9, 2019
.gitignore
LICENSE
README.md
appveyor.yml
build.cake Enable tests on CI Mar 12, 2019

README.md

ReSharper Structured Logging

Build status Quality Gate Status

An extension for ReSharper that highlights structured logging templates and contains some useful analyzers

At the moment it supports Serilog, NLog, and Microsoft.Extensions.Logging

Installation

Look for Structured Logging in ReSharper -> Extension manager.
JetBrains Plugins Repository

Highlighting

Highlighting

Analyzers

Duplicate template property

Noncompliant Code Example:

Log.Error("Disk quota {Quota} MB exceeded by {Quota}", quota, user);

Compliant Solution:

Log.Error("Disk quota {Quota} MB exceeded by {User}", quota, user);

Exception passed as a template argument

Noncompliant Code Example:

catch (Exception exception)
{
   Log.Error(ex, "Disk quota {Quota} MB exceeded {Exception}", quota, exception);
}

Compliant Solution:

catch (Exception exception)
{
   Log.Error(exception, "Disk quota {Quota} MB exceeded", quota);
}

Message template is not a compile time constant

Noncompliant Code Examples:

Log.Error($"Disk quota {quota} MB exceeded by {user}");
Log.Error(string.Format("Disk quota {0} MB exceeded by {1}", quota, user));

Compliant Solution:

Log.Error("Disk quota {Quota} MB exceeded by {User}", quota, user);

Anonymous objects must be destructured

Noncompliant Code Examples:

Log.Error("Processed {Position}", new { x = 4, y = 2});

Compliant Solution:

Log.Error("Processed {@Position}", new { x = 4, y = 2});

Incorrect type is used for contextual logger

Noncompliant Code Examples:

class A
{
    private static readonly ILogger Logger = Logger.ForContext<B>();
}

class B {} 
class A
{
	ILogger<B> _log;
	
	public A(ILogger<B> log)
	{
		_log = log;
	}
}

class B { } 

Compliant Solution:

class A
{
    private static readonly ILogger Logger = Logger.ForContext<A>();
}

class B {} 
class A
{
	ILogger<A> _log;
	
	public A(ILogger<A> log)
	{
		_log = log;
	}
}

class B {} 

Credits

Inpired by SerilogAnalyzer

You can’t perform that action at this time.