Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog File target and keepFileOpen flag

Tags:

c#

nlog

A FileTarget object in NLog has a property (bool) called "keepFileOpen". By default this property is set to false, meaning that each log event will open the file, write in the file and close the file.

The hit in performance is huge, so I specified "keepFileOpen" to true, instead of false, meaning that the file will be opened only once.

Does anybody has an idea (or just know) why this property is by default set to "false", incuring a HUGE performance hit on logging ?

Is there any scenarios where setting this property to true can cause issues (which would therefore make sense to default it to "false").

Thanks !

EDIT

Performance measurements for basic layout, writing 100K events to the logger :

  • keepFileOpen = false (default) : ~ 101 sec
  • keepFileOpen = true : ~ 1 sec
like image 275
darkey Avatar asked Jul 09 '12 21:07

darkey


People also ask

What is target NLog?

Targets - the destinations of a logevent, e.g. file, database, console. Layout - the layout e.g. json, csv, plain-text (default)

What is NLog config in C#?

NLog is a . Net Library that enables you to add high-quality logs for your application. NLog can also be downloaded using Nugget in Visual Studio. Targets are used to display, store, or pass log messages to another destination.

Which of the following logging levels does NLog support?

NLog supports semantic/structured logging known from similar frameworks like Serilog and Microsoft. Extensions. Logging. With structured logging, you no longer log just simple text messages.


1 Answers

I would say that closing the file is the expected behaviour. If you will try to access the file from another process or delete it in file system while the NLog-process is alive it would lead to those annoying system-errors saying that some process holds the file etc.

If the time of opening a file is too long for you, try using the AsyncWrapper and you'll get a fire-and-forget-behavior.

Therefore I think that the default value is ok.

like image 179
Alexander Schmidt Avatar answered Nov 10 '22 06:11

Alexander Schmidt