kseen
kseen

Reputation: 397

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.

Upvotes: 4

Views: 1842

Answers (2)

Rob
Rob

Reputation: 6871

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.

Upvotes: 9

Pure.Krome
Pure.Krome

Reputation: 86937

Like Rob has answered .. stick with NLog :)

If you need a good viewer, don't forget to use Sentinal as a free nLog viewer :)

Don't forget, you can programatically turn on logging sections, while in production .. to view specific areas of the website. And then see them in real-time using Sentinal.

enter image description here


another option is to take advanctage of something like LoggR.net. This streams data (ie. errors or logging information in this case) in real time. I think it's using the awesome SignalR framework to use WebSockets or Long Polling (2 real-time technologies) :)

enter image description here

Upvotes: 7

Related Questions