New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers 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
Write exceptions in InvariantCulture to log file #172
Comments
I can see want you mean, but what part of the exception do you need to be invariant culture? Isn't this a some the exception should contain? |
Seems to a duplicate #152 |
This is an old issue but it seems the fix done for #152 doesn't work. The problem is when using a layout like This behavior occurs even when setting Now the workaround is to switch the |
Hi @srollinet, |
Hi @304NotModified , I'm not sure it is the same. What I expect is the possibility to have exactly the same exception message + stack trace regardless of the CurrentUICulture Something like that:
|
@srollinet I added the unit tests to #1556 but it works for me locally (let see what appVeyor tells) #1556 didn't changed the ExceptionLayoutRenderer, so it also works on master then. Can you give an example of the visual differences you get? (please do a toString on |
Tested with V4.3.6 (oops, I haven't seen that #1556 is not part of this release) so maybe the problem is simply here...
|
@srollinet is it possible to test with a NuGet beta package? (will create one then) |
Dived into to this, it seems not to be possible to pass the culture to the exception.toString or something like that. See http://stackoverflow.com/questions/209133/exception-messages-in-english |
@304NotModified I cloned the repository and the test fails as well. So yes it is probably because I have non-english languages installed. I have seen the StackOverflow question and also asked a new one specific to NLog http://stackoverflow.com/questions/38585028/configure-nlog-to-write-exception-messages-in-english As I say in my question, I wanted to use a better solution than switching manually the CurrentUICulture. But if it is not possible I will probably wrap the logger and switch the culture. It won't be hard as I inject it as an ILogger |
the fancy way to fix is, is writing a LayoutWrapper which switches the Culture. Something like this: using System.Globalization;
using System.Threading;
using NLog.Layouts;
namespace NLog.LayoutRenderers.Wrappers
{
using NLog.Conditions;
using NLog.Config;
[LayoutRenderer("SwitchCulture")]
[AmbientProperty("Culture")]
[ThreadAgnostic]
public sealed class SwitchCultureLayoutRendererWrapper : WrapperLayoutRendererBase
{
[RequiredParameter]
public CultureInfo Culture { get; set; }
/// <summary>
/// Transforms the output of another layout.
/// </summary>
/// <param name="text">Output to be transform.</param>
/// <returns>Transformed text.</returns>
protected override string Transform(string text)
{
return text;
}
/// <summary>
/// Renders the inner layout contents.
/// </summary>
/// <param name="logEvent">The log event.</param>
/// <returns>
/// Contents of inner layout.
/// </returns>
protected override string RenderInner(LogEventInfo logEvent)
{
var oldCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = Culture;
//maybe also a new thread ?
try
{
var value = base.RenderInner(logEvent);
return value;
}
finally
{
Thread.CurrentThread.CurrentUICulture = oldCulture;
}
}
}
} |
@304NotModified Okay, I like this idea because it doesn't involve too much code :) But I am probably missing something. I have the following exception: "LayoutRenderer cannot be found: 'InvariantCulture'" using System.Globalization;
and the config
|
You need to register it manually or creating an assembly. e.g. ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("hello-universe", typeof(MyNamespace.HelloUniverseLayoutRenderer See http://nlog-project.org/2015/06/30/extending-nlog-is-easy.html PS; no the namespace is not mandatory |
Works like a charm. Thanks for your help |
Glad to hear! |
This should be part of NLog! |
we don't like to change the global "Thread.CurrentThread" in NLog. Sounds like a bad idea, so not supported out-of-the-box. |
Thx for library and suggested solution of the problem. @304NotModified |
If you ever got a log file from Japan you got the problem.
Exceptions (and other strings that come from a resource) should be written to the log file with the CultureInfo.InvariantCulture.
The text was updated successfully, but these errors were encountered: