I have an installable ASP.Net application that is logging using Common.Logging.
When writing log messages ie:
_log.InfoFormat(CultureInfo.???, "{0}: Writing to the Log", DateTime.UtcNow);
I am confused about which culture I should be using. The result I want is that the log messages are formatted using the server’s regional settings. That way when administrators install the application they have control over how their log messages are formatted.
There are fours options but none seem to be the correct choice:
- CultureInfo.CurrentCulture: this seems like it would be the correct choice for a windows app but ASP.Net is changing this to match the user browsing the site – so it’s appropriate for formatting content to display to the user but not for writing to the log.
- CultureInfo.CurrentUICulture: similar problems to CurrentCulture – will match the current user browsing the site.
- CultureInfo.InstalledUICulture: the system installed culture – appears to relate to the copy of windows installed but not alterable by the user. Apparently this one is a bit useless
- CultureInfo.InvariantCulture: this is not user configurable. I am wondering if this is the best of the four options here though because at least it is consistent and won’t vary by who is browsing the site/the media windows was installed from.
I am left wondering if I need to add explicit configuration in my web.config for the system culture so the installer can choose a culture. The answer I really want is whatever CultureInfo.CurrentCulture was before ASP.Net changed it to match the current user.
Note: I know I can turn off ASP.Net changing the CurrentCulture per user but the UI relies on this behaviour.
Admins like to have control over the logs. Telling them to go configure their server culture so that the logs are in a particular format doesn’t seem like the best solution to me.
Adding a configuration option into web.config is a good idea. This will let the admin configure the log format as required.
Rather than ask the user, you could make an intelligent guess while installing using
CultureInfo.CurrentCulture. If the user wants it to be something different, he can always change it.If you do want to take it from
CultureInfo.CurrentCulturerather than configuration, you can pick it during theApplication_Startevent, and keep it in the application context.