1

I am trying out a scenario where in I am able to generate the JSON logs and store them in database.

I have to use log4net as logging mechanism. So far I am able to achieve the log4net Json using the json formater as below.

<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="C:\\TestProj\\jsonlog.txt" />
    <param name="AppendToFile" value="true" />
    <param name="DatePattern" value="_yyyyMMddHH&quot;.log&quot;" />
    <param name="RollingStyle" value="Date" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
    </layout>
</appender>
<root>
    <level value="ALL" />
    <appender-ref ref="FileAppender" />
</root>

I also know how to insert the log4net logs into SQL using adonetappender.

However I am not able to figure out these two questions:

  1. How to insert the json logs into sql server database.
  2. How to insert the json logs into a no-sql database.
stuartd
  • 70,509
  • 14
  • 132
  • 163
Abi P
  • 1,410
  • 3
  • 22
  • 36
  • For 1), just configure the ADO.Net appender appropriately, and for 2) it depends which db you use, for example for MongoDB you could use [tog4mongo-net](https://github.com/log4mongo/log4mongo-net) – stuartd Apr 15 '15 at 22:21

3 Answers3

1

I think I got it. I use regular ado.net appender and then use json layout for one of the parameters. –

Abi P
  • 1,410
  • 3
  • 22
  • 36
0

I think you are not storing your logs in to the actual json file, you are using jsonlog.txt which is a text file. If you want to store the jsonlogs to database you can configure log4net to do it for you. Take a look at this.

https://logging.apache.org/log4net/release/config-examples.html

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • Hi,I am not sure how to do that. For json output I have to use json appender. And then how will I configure the adonet part of it to insert into database? – Abi P Apr 17 '15 at 15:07
  • Hi,I think I got it. I use regular ado.net appender and then use json layout for one of the parameters. – Abi P Apr 17 '15 at 15:14
0

Inserting into no-sql was the primary reason for SerializedLayout to happen. There's multiple ways this can be achieved, but you'll most likely want some intermediate processor for the logs. I can recommend logstash and nxlog. Logstash can easily store your files in elastic search then.

They can both retrieve logs from:

  1. Files
  2. Network
    1. TCP
    2. UDP
    3. Syslog

Other options would be queues, like RabbitMQ or AMQP. I haven't played with those. It's up to your needs and resources with regards to availability and resilience.

With regards to formatting the JSON bunch, see the answer on another question of yours - https://stackoverflow.com/a/36169213/481812

Community
  • 1
  • 1
Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42