We have 2 targets in our default NLog config: File
and Console
. In order for the Console to stay readable we would like to define a maximum length of a line shown in the Console before wrapping to the next line.
Now I have looked quite a bit at the different layouts for NLog but could only find the pad wrapper with a fixed-length
option. But this truncates the line instead of wrapping it.
The only way I can think of is via regexp and introduction of the${newline}
layout.
Any other ideas?
You could write your own wrapper, along the lines of:
[LayoutRenderer("wrap-line")]
[AmbientProperty("WrapLine")]
[ThreadAgnostic]
public sealed class WrapLineRendererWrapper : WrapperLayoutRendererBase
{
public WrapLineRendererWrapper ()
{
Position = 80;
}
[DefaultValue(80)]
public int Position { get; set; }
protected override string Transform(string text)
{
return string.Join(Environment.NewLine, MakeChunks(text, Position));
}
private static IEnumerable<string> MakeChunks(string str, int chunkLength)
{
if (String.IsNullOrEmpty(str)) throw new ArgumentException();
if (chunkLength < 1) throw new ArgumentException();
for (int i = 0; i < str.Length; i += chunkLength)
{
if (chunkLength + i > str.Length)
chunkLength = str.Length - i;
yield return str.Substring(i, chunkLength);
}
}
}
How to use it : https://github.com/NLog/NLog/wiki/Extending%20NLog#how-to-write-a-custom-layout-renderer
Based on this one : https://github.com/NLog/NLog/blob/master/src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs
And this answer : https://stackoverflow.com/a/8944374/971
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.