Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application logging architecture

I'm writing an ASP .NET MVC3 application and as application is expected to be secure, I need a good enterprise application logging architecture.

So I was seeking over existing loggin frameworks and picked the NLog. So at them moment I'm stuck with making DB schema for logs.

Does anyone have good experience in this area? It's anticipate to log a set of actions such as user interactions with system objects, background works, user membership actions, payment transactions and so on.

like image 600
kseen Avatar asked Dec 05 '22 19:12

kseen


1 Answers

I've been using NLog for a while now and i'm very happy with it. What I most like about NLog is that you can configure different loglevels to be written to different files and/or databases. It's a very powerfull logging library.

For logging to the database you can use something like below in your config. This is similar to what I use at the company i'm working.

<target xsi:type="Database" 
        name="TestDatabaseLogging" 
        connectionString="Data Source=127.0.0.1;Initial Catalog=NLog_Test;User ID=MyLogin;Password=MyPassword" 
        dbDatabase="NLog_Test">
  <commandText>
    insert into INNO_LOG ([createDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace)
  </commandText>
  <parameter name="@createDate" layout="${date}"/>
  <parameter name="@origin" layout="${callsite}"/>
  <parameter name="@logLevel" layout="${level}"/>
  <parameter name="@message" layout="${message}"/>
  <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/>
  <parameter name="@stackTrace" layout="${stacktrace}"/>
</target>

You can use the rules section to log different levels to different files, see example below;

  <rules>
    <logger name="*" minlevel="Fatal" writeTo="mail" />
    <logger name="*" minlevel="Error" writeTo="TestDatabaseLogging" />
    <logger name="*" minlevel="Debug" writeTo="file-debug" />
    <logger name="*" minlevel="Info" writeTo="file" />
    <!--Log to specific files for specific classes.-->
    <logger name="_Default" minlevel="Trace" writeTo="file-default" />
    <logger name="TestClass" minlevel="Trace" writeTo="file-testclass" />
  </rules>

EDIT: Added possible table layout for logging information.

Id | int
CreateDate | datetime
LogLevel | nvarchar
Message | nvarchar(max)
Exception | nvarchar(max)
StackTrace | nvarchar(max)
SourceUrl | nvarchar(255)
UserId | uniqueidentifier
OrderId | int

The layout above is just an rough idea. It totally depends on what you want to log in this table. Though you have to try if it's possible to add additional paramaters other than the ones used by NLog by default.

like image 113
Rob Avatar answered Dec 21 '22 23:12

Rob