I define all NLog instances in the solution like this:
private readonly static Logger logger = LogManager.GetCurrentClassLogger();
In all NLog examples, the logger field is Camel cased: http://www.codeproject.com/Articles/10631/Introduction-to-NLog
However, in Microsoft documentation they recommend to name all readonly static fields using Pascal case: https://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.100%29.aspx
In addition StyleCop has a rule with the same Pascal convention: http://www.stylecop.com/docs/SA1311.html
So what would be the best convention for this scenario?
The naming conventions always seemed a bit blurry to me, and Microsoft doesn't seem to follow them to the letter too.
Based on Resharper's naming conventions for private fields (which are based on StyleCop I believe):
-private static readonly Type Name
-private readonly Type _name
I personally usually go with the naming conventions offered by Resharper because they seem to follow Microsoft's naming as closely as it can, and it offers consistency for the whole codebase (if you screw it up, it will tell you).
EDIT: Others have noted, coding standards are kind of like religion. So I have my personal preference below (based on my experience and research over the years).
I personally like private, static
to be lowercase and backing fields prefixed with an underscore, like below:
private static readonly Type log
private readonly Type _log
Since you mentioned StyleCop, if you're just getting started, I'd stick with what StyleCop has by default and see if you like it. Personally, I use .editorconfig with all my projects so there's no confusion between developers. The config below is for setting the above suggestions (and disabling the StyleCop rules).
[*.{cs,vb}]
# camelCase and PascelCase
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
dotnet_naming_style.camel_case_style.capitalization = camel_case
# camelCase with _ prefix
dotnet_naming_style._camelCase.capitalization = camel_case
dotnet_naming_style._camelCase.required_prefix = _
# Private Static Fields is camelCase (e.g. log)
dotnet_naming_symbols.private_static_field_symbol.applicable_kinds = field
dotnet_naming_symbols.private_static_field_symbol.applicable_accessibilities = private
dotnet_naming_symbols.private_static_field_symbol.required_modifiers = static
dotnet_naming_rule.private_static_field_naming.symbols = private_static_field_symbol
dotnet_naming_rule.private_static_field_naming.style = camel_case_style
dotnet_naming_rule.private_static_field_naming.severity = suggestion
# SA1311: Static readonly fields should begin with upper-case letter
dotnet_diagnostic.SA1311.severity = none
# Private Fields is _camelCase (backing fields)
dotnet_naming_symbols.private_field_symbol.applicable_kinds = field
dotnet_naming_symbols.private_field_symbol.applicable_accessibilities = private
dotnet_naming_rule.private_field_naming.symbols = private_field_symbol
dotnet_naming_rule.private_field_naming.style = _camelCase
dotnet_naming_rule.private_field_naming.severity = suggestion
# SA1309: Field names should not begin with underscore
dotnet_diagnostic.SA1309.severity = none
P.S I also prefer the log
name over logger
which was the standard with log4net
. With newer logging libraries, that's changed to logger
.
I personally prefer reading log.Info(...)
, but I've used logging libraries with methods .LogInfo(...)
. In that case, I use logger.LogInfo(...)
. I believe the standard has changed since Microsoft.Extensions.Logging.Abstractions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With