SerilogAnalyzer
SerilogAnalyzer copied to clipboard
How to trigger refactoring?
First of all, I gotta say how amazing this analyzer is! Thank you for building it!
I cannot figure out how to trigger the refactoring of the LoggerConfiguration though. Judging by the gif in the README, it looks like you must just put the cursor before the parens on CreateLogger() and hit Ctrl+., but that doesn't work for me. I have tried all various ways to trigger and it just ain't workin.
What might I be missing?
Can you post a snippet on which it does not work?
Holy smokes, that was a fast response!
var logger = new LoggerConfiguration()
.Enrich.WithThreadId()
.Enrich.WithEnvironmentUserName()
.WriteTo.Console(
outputTemplate: "{Timestamp:s} {Level:u12} [{ThreadId}] [{SourceContext}] [{EnvironmentUserName}]{NewLine}{Message}{NewLine}---------------{NewLine}",
theme: AnsiConsoleTheme.Code)
.CreateLogger();
It does work for me on this snippet, you can place the cursor anywhere between new and ;.
Are you using VS (2017 / 2019?) or VS Code? Nuget version or the VSIX extension?
It's running into a limitation though on
AnsiConsoleTheme.Code-> Can't statically determine value of expression
"Serilog": {
"Using": ["Serilog.Enrichers.Environment", "Serilog.Enrichers.Thread", "Serilog.Sinks.Console"],
"WriteTo": [
{ "Name": "Console", "Args": { "outputTemplate": "{Timestamp:s} {Level:u12} [{ThreadId}] [{SourceContext}] [{EnvironmentUserName}]{NewLine}{Message}{NewLine}---------------{NewLine}", "theme": "?" } }
],
"Enrich": ["WithEnvironmentUserName", "WithThreadId"]
}
<add key="serilog:enrich:WithEnvironmentUserName" />
<add key="serilog:enrich:WithThreadId" />
<add key="serilog:write-to:Console.outputTemplate" value="{Timestamp:s} {Level:u12} [{ThreadId}] [{SourceContext}] [{EnvironmentUserName}]{NewLine}{Message}{NewLine}---------------{NewLine}" />
<add key="serilog:write-to:Console.theme" value="?" />
<add key="serilog:using:Environment" value="Serilog.Enrichers.Environment" />
<add key="serilog:using:Thread" value="Serilog.Enrichers.Thread" />
<add key="serilog:using:Console" value="Serilog.Sinks.Console" />
Here's the details on my VS Installation. It's the most up to date version of 2017.
Microsoft Visual Studio Enterprise 2017 Version 15.9.5 VisualStudio.15.Release/15.9.5+28307.280 Microsoft .NET Framework Version 4.7.03056 Installed Version: Enterprise
Are you using the nuget version? It seems that while Analyzers and CodeFixes work, CodeRefactorings do not. If you're using the nuget version, you should be able to workaround the issue by installing from here https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer
Oh yeah, I am using the Nuget version. I just forked the project and will try debugging the issue and try to provide some further info. Although, that will just use the VSIX extension and maybe it won't be a problem... At any rate, I've got a bit of recent experience working with developing analyzers. I'll see what I can see.
When I run it through the debugger it shows me the refactoring which appears to include an error. Not sure if that's an issue when running it from nuget? Here's the output:
/*
Errors:
C:\Code\----redacted----\Configuration\SerilogConfig.cs: (18,27)-(18,48): `AnsiConsoleTheme.Code` -> Can't statically determine value of expression
"Serilog": {
"Using": ["Serilog.Enrichers.Environment", "Serilog.Enrichers.Thread", "Serilog.Sinks.Console"],
"WriteTo": [
{ "Name": "Console", "Args": { "outputTemplate": "{Timestamp:s} {Level:u12} [{ThreadId}] [{SourceContext}] [{EnvironmentUserName}]{NewLine}{Message}{NewLine}---------------{NewLine}", "theme": "?" } }
],
"Enrich": ["WithEnvironmentUserName", "WithThreadId"]
}
*/
@mikesigs that is nothing to worry about (in terms of analyzing), that just means that Code is a getter on AnsiConsoleTheme for which it cannot generate a constant value that can be copied into the config.
In my own analyzer I added some custom logging (writing to a file basically) to help diagnose some issues. I am going add that in here and see what it produces from the Nuget version.
Also, it looks like you can put the themes in configuration like this:
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
Totally unrelated, but perhaps a feature suggestion ;)
Yes i need to catch up with improvements to the config system of serilog 😉
It's possible I am not nuget packing this thing correctly. And I've never worked with PCL's before.
But when I create a nuget package locally and try to reference it, I get a bunch of warnings stating that Microsoft.CodeAnalysis... 1.0.0 was restored using .NETFramework 4.6.1 instead of .NETCoreApp.
But when I use your version from Nuget, it doesn't display those warnings.
But perhaps this is a .NET Core compatibility thing underneath somehow??
If that's the case, it might require updating to .NET Standard.
Let me know if you'd like some help with that.
For now, I am gonna just move forward without the refactoring part. Or... if I need it, I'll install the VSIX.
So actually this is by design from roslyn, code refactorings only get loaded from VSIX packages, see explanation here https://github.com/dotnet/roslyn/issues/32705#issuecomment-456987581