Nlog: LogBuilder from Fluent throws exception when used wrongly

Created on 4 Aug 2020  ·  8Comments  ·  Source: NLog/NLog

NLog version: 4.7.2

Platform: .Net 4.0

The fluent API can be misused, and in that case it throws an exception. For example, I (wrongly) assumed that I should put the log text immediately in Info:

Log.Info(jsonText);

I didn't read up on the documentation properly. But, this now failed with an exception:

System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.GetFileName(String path)
   at System.IO.Path.GetFileNameWithoutExtension(String path)
   at NLog.Fluent.Log.Create(LogLevel logLevel, String callerFilePath)

I'm coming from log4net where exceptions are never thrown by the logging framework. This lack of error handling takes away quite a bit from NLog, unfortunately (IMO of course).
I can think of a simple error handling in this case. If the callerFilePath can't be parsed, use a default or omit entirely. But never throw an exception. Please 🙇‍♂️

bug

Most helpful comment

Hoping to replace Fluent-API with something better in NLog 5.0 (#4044). But yes exceptions should not be thrown, even when providing unexpected input. Created #4069

All 8 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

Hoping to replace Fluent-API with something better in NLog 5.0 (#4044). But yes exceptions should not be thrown, even when providing unexpected input. Created #4069

Note that NLog do have some input validation. Ex. these calls will throw:

```c#
string loggerName = null;
LogManager.GetLogger(loggerName);

NLog.LogLevel logLevel = null;
LogManager.GetLogger("Test").Log(logLevel, "Hello");

NLog.LogLevel logLevel = LogLevel.FromString(null);
```

Examples of NLog Fluent can be found here:

https://github.com/NLog/NLog/wiki/Fluent-API

Yes I did find the docs when I realised it wasn't doing at all what I thought. I actually thought the global Log instance would replace all my local, static readonly instances, with no syntax differences in the actual logging calls. That was great, I thought.

Resolved with #4069

Hi! NLog 4.7.4 has been released! https://www.nuget.org/packages/NLog/4.7.4

Good to hear!

/Daniel
On 22 Aug 2020, 22:22 +0200, Julian Verdurmen notifications@github.com, wrote:

Hi! NLog 4.7.4 has been released! https://www.nuget.org/packages/NLog/4.7.4

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rapiiidooo picture Rapiiidooo  ·  3Comments

MaximRouiller picture MaximRouiller  ·  3Comments

haythamabutair picture haythamabutair  ·  3Comments

JustArchi picture JustArchi  ·  3Comments

vasumsit picture vasumsit  ·  3Comments