I have two targets in my nlog.config file
<targets>
<target xsi:type="File"
name="logfile"
fileName="../../../Log/log.log">
<layout xsi:type="Layout">
<renderer type="truncate" maxMessageLength="500" />
</layout>
</target>
<target xsi:type="Console"
name="logconsole"
layout="${truncate:MaxLength=20}">
</target>
</targets>
None of them works, because of custom renderer class, but all code I wrote based on manuals and chatgpt.
Here is my class
[LayoutRenderer("truncate")]
public class TruncateLayoutRenderer : LayoutRenderer
{
[RequiredParameter]
public int MaxLength { get; set; }
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
string formattedLogEntry = $"test {logEvent.TimeStamp:yyyy-MM-dd HH:mm:ss} {logEvent.Level:uppercase=true} - {logEvent.FormattedMessage}";
if (formattedLogEntry.Length > MaxLength)
{
formattedLogEntry = formattedLogEntry.Substring(0, MaxLength);
}
builder.Append(formattedLogEntry);
}
}
Config file and class in in one project
When I launch project I see that layout is set on default. But if I use layout hardcoded in config file it works. What is wrong with my class?
I read manuals and asked gpt, it seems that everything is coorect
If you read the wiki-page How to write a custom layout renderer, then it says Don't forget to register your custom component
But notice there already exist a built-in truncate-method in NLog. So you can just do this ${message:truncate=500}
(without needing your own custom layout renderer.
See also: https://nlog-project.org/config/?tab=layout-renderers