-
Notifications
You must be signed in to change notification settings - Fork 30
New Rule: Consistent Naming for Logged Types #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its m 8000 aintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Consistent naming is something that bothered me for some while now. |
This could be a bit problematic for things like: Account from; to;
Log.Information("Transferring {Amount} from {From} to {To}", 100, from, to); Seems like the hit rate for this rule might be patchy. |
I would be happy to see this rule only apply to reference types. I think
Nicholas's example is salient.
…On Mar 30, 2017 4:12 PM, "Nicholas Blumhardt" ***@***.***> wrote:
This could be a bit problematic for things like:
Account from; to;Log.Information("Transferring {Amount} from {From} to {To}", 100, from, to);
Seems like the hit rate for this rule might be patchy.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#17 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABj7xCZSQonuSo_GkG7ZK9ekFfqiWVLBks5rrAzEgaJpZM4Muvos>
.
|
I think this type of feature would be really useful! This would be a good option for all Properties to make sure they are the same type. I've been planning out an integration with ElasticSearch and Kibana, the big thing would be making sure that Properties with the same name have the same type so they can be properly indexed. E.g. If it's a number then always a number, same for date, same for string, etc. This would also apply for complex types being serialised. A second helper could be that if you are logging a reference type, that it force (or suggest) using a particular name to keep things consistent. The level, e.g. suggest, warn, force could be per type. You could configure the types in a JSON doc as such: You would assume a TypesToNames entry would also be the equivalent of adding it to the NamesToType list. This could be done in code to avoid duplicates in the file. If the analyzer doesn't find the keyword in the list it could offer an auto-complete to add to the JSON document. I'd be willing to help with this, but not 100% sure where to start. |
@kevygreen You could start around in this region for the analysis part: SerilogAnalyzer/SerilogAnalyzer/SerilogAnalyzer/DiagnosticAnalyzer.cs Lines 170 to 239 in b78fb7f
The configuration bit is tricky, roslyn has nothing to offer for this, but i've been thinking that if you manage to get the file path of the file you're analyzing (perhaps from the SyntaxNodeAnalysisContext.Compilation), then look for .seriloganalyzer (similiar to .editorconfig) in the current directory and if not, check the parent directory and so on. I just hope this doesn't affect performance too bad if you do this for every logger statement you encounter in a document.
|
@Suchiman is there any mechanism for maintaining state across files being analyzed or do the analyzers only work one file at a time, in process isolation? If not, a global static might be one way to improve performance and cache the config? |
@normanhh3 there's no builtin analyzer mechanism for maintaining state. Depends on what sort of analyzer you write, usually they operate on one file or one syntax location though and they might be executed out of process (VS) or inside the csc.exe. As long as it's running inside VS, you can MEF Import visual studio services into the analyzer. |
In order to get the most value out of the log entries, property names need to be consistent for object types that are logged.
For example, logging a type User as user, User and uSer all make it MUCH harder to filter the log entries.
My proposal is that a rule ensure that logged property names are based on the type that is being logged by default.
It would be good to include a SerilogPropertyNameToTypeMapping.json that would allow for overriding the "standard" mapping from a type to a property name.
Does this sound like something that fits within the vision of this tool?
The text was updated successfully, but these errors were encountered: