Nlog: How to log string without surrounded double quotes ?

Created on 21 Aug 2019  路  4Comments  路  Source: NLog/NLog

string m = "message";
logger.Debug("A message: {Message}.", m);

output:
A message: "message".

question

Most helpful comment

You can also do use the :l format parameter (l = literal) to avoid the quotes:

```c#
string m = "message";
logger.Debug("A message: {Message:l}.", m);


But yes you can make it application wide by replacing  `IValueFormatter` like this:

```c# 
        class OverrideValueFormatter : IValueFormatter
        {
            private IValueFormatter _originalFormatter;

            public bool StringWithQuotes { get; set; }

            public OverrideValueFormatter(IValueFormatter originalFormatter)
            {
                _originalFormatter = originalFormatter;
            }

            public bool FormatValue(object value, string format, NLog.MessageTemplates.CaptureType captureType, IFormatProvider formatProvider, System.Text.StringBuilder builder)
            {
                if (!StringWithQuotes && captureType == NLog.MessageTemplates.CaptureType.Normal)
                {
                    switch (Convert.GetTypeCode(value))
                    {
                        case TypeCode.String:
                            {
                                builder.Append((string)value);
                                return true;
                            }
                        case TypeCode.Char:
                            {
                                builder.Append((char)value);
                                return true;
                            }
                        case TypeCode.Empty:
                            return true;  // null becomes empty string
                    }
                }

                return _originalFormatter.FormatValue(value, format, captureType, formatProvider, builder);
            }
        }

And register it like this:

c# var oldFormatter = NLog.Config.ConfigurationItemFactory.Default.ValueFormatter; var newFormatter = new OverrideValueFormatter(oldFormatter); newFormatter.StringWithQuotes = false; NLog.Config.ConfigurationItemFactory.Default.ValueFormatter = newFormatter;

All 4 comments

You need to create a custom IValueFormatter for that and set
c# ConfigurationItemFactory.Default.ValueFormatter

Another option is by not using structured logging:

logger.Debug("A message: {0}.", m);

You can also do use the :l format parameter (l = literal) to avoid the quotes:

```c#
string m = "message";
logger.Debug("A message: {Message:l}.", m);


But yes you can make it application wide by replacing  `IValueFormatter` like this:

```c# 
        class OverrideValueFormatter : IValueFormatter
        {
            private IValueFormatter _originalFormatter;

            public bool StringWithQuotes { get; set; }

            public OverrideValueFormatter(IValueFormatter originalFormatter)
            {
                _originalFormatter = originalFormatter;
            }

            public bool FormatValue(object value, string format, NLog.MessageTemplates.CaptureType captureType, IFormatProvider formatProvider, System.Text.StringBuilder builder)
            {
                if (!StringWithQuotes && captureType == NLog.MessageTemplates.CaptureType.Normal)
                {
                    switch (Convert.GetTypeCode(value))
                    {
                        case TypeCode.String:
                            {
                                builder.Append((string)value);
                                return true;
                            }
                        case TypeCode.Char:
                            {
                                builder.Append((char)value);
                                return true;
                            }
                        case TypeCode.Empty:
                            return true;  // null becomes empty string
                    }
                }

                return _originalFormatter.FormatValue(value, format, captureType, formatProvider, builder);
            }
        }

And register it like this:

c# var oldFormatter = NLog.Config.ConfigurationItemFactory.Default.ValueFormatter; var newFormatter = new OverrideValueFormatter(oldFormatter); newFormatter.StringWithQuotes = false; NLog.Config.ConfigurationItemFactory.Default.ValueFormatter = newFormatter;

Thanks all. Great solutions. 馃尮

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imanushin picture imanushin  路  3Comments

imanushin picture imanushin  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

haythamabutair picture haythamabutair  路  3Comments

sszost picture sszost  路  3Comments