Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog. Write logs into different tables

I have a ASP MVC project with areas and i have to log exceptions into database. But I have a problem. I must write log exceptions from each area to its own table in database. I have an idea to configure DataTargets for all areas with CommandText related with table name, but it seems pretty ugly. A bit more fantastic way is to get all areas on application start and build targets via some factory.

Does someone have any idea or best practices about this issue?

like image 748
user3272018 Avatar asked Sep 01 '16 09:09

user3272018


People also ask

How do you use NLog for multiple projects in the same solution?

How do you use NLog for multiple projects in the same solution? If you want to log in other projects within the solution, just reference NLog. dll directly in each of the projects and use it. But now you only need to configure NLog in your main application.

Is NLog asynchronous?

NLog 1.0 supports asynchronous logging, but there is no good support for asynchronous exception handling. This is because wrappers targets are not capable of receiving exceptions which are raised on other threads.

Where are NLog logs stored?

nlog file can be found at: C:\Program Files (x86)\Pleasant Solutions\Pleasant Password Server\www\web. nlog.


1 Answers

The CommandText property of the database target is a layout, so you could do:

<target xsi:type="Database"
      name="db"
      commandText="insert into ${event-properties:tablename} ... ">

and using fluent style (NLog.Fluent namespace):

logger.Info().Message("this is a message").Property("tablename", "table1").Write();

You could also use more global contexts, like GDC, MDC etc. See NLog Wiki

like image 159
Julian Avatar answered Sep 22 '22 14:09

Julian