question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to log string without surrounded double quotes ?

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

output: A message: "message".

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
snakefootcommented, Aug 21, 2019

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

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

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

        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:

        var oldFormatter = NLog.Config.ConfigurationItemFactory.Default.ValueFormatter;
        var newFormatter = new OverrideValueFormatter(oldFormatter);
        newFormatter.StringWithQuotes = false;
        NLog.Config.ConfigurationItemFactory.Default.ValueFormatter = newFormatter;
0reactions
rgbavcommented, Mar 24, 2023

NLog.Config.ConfigurationItemFactory.Default.ValueFormatter is deprecated.

A new way to add the OverrideValueFormatter from @snakefoot would be to define a static method In the Formatter:

public static LoggingConfiguration AssignToConfiguration(LoggingConfiguration configuration)
{
	var originalFormatter = configuration.LogFactory.ServiceRepository.GetService(typeof(IValueFormatter)) as IValueFormatter;
	var newFormatter = new OverrideValueFormatter(originalFormatter)
	{
		StringWithQuotes = false
	};
	configuration.LogFactory.ServiceRepository.RegisterService(typeof(IValueFormatter), newFormatter);

	return configuration;
}

This can than be called f.e. with LogManager.Configuration = OverrideValueFormatter.AssignToConfiguration(LogManager.Configuration);

Read more comments on GitHub >

github_iconTop Results From Across the Web

I want to remove double quotes from a String
Assuming: var someStr = 'He said "Hello, my name is Foo"'; console.log(someStr.replace(/['"]+/g, ''));. That should do the trick.
Read more >
Remove Beginning and Ending Double Quotes from a String
In this tutorial, we'll study different approaches for removing beginning and ending double quotes from a String in Java.
Read more >
bash - Why quotes are retained in string variables when ...
A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless...
Read more >
JavaScript Strings: The Basic Methods and Functions
Strings in JavaScript are contained within a pair of either single quotation marks '' or double quotation marks "". Both quotes represent Strings...
Read more >
How to remove Quotes from a String in JavaScript
If you need to remove the double and single quotes from a string, use the logical AND (&&) operator. index.js. Copied! const str...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found