Skip to main content

How to use SQLite Logger

First of all, you should know that this Logger is suitable for projects using .Net framework 4.7.2+

This logger consists of two Nuget Packages, SQLiteLogger and SQLiteLoggerViewer. This is mainly to fit project's structure as SQLiteLoggerViewer will be included in Application Layer and SQLiteLogger will be included in Application or Business Layer.

SQLiteLogger

The main Logger which used to log messages. To use it follow these steps:
  • Install SQLiteLogger Nuget Package.
  • Download log file from this link.
  • In your configuration file (web.config) please add the following App settings:
    • LiteLogFilePath: Path of Log file that you downloaded with respect to project folder. Make sure to add this key to avoid exceptions.
      • <add key="LiteLogFilePath" value="<Path-to-log-file>"/>
    • LiteLogLevel: Level of logs you want to be logged. You may choose [All-Info-Warn-Debug-Error]. Make sure to add this key as the logger will not log anything in case this key is missed.
      • <add key="LiteLogLevel" value="All"/>
Now, you can use any of the logging functions:
  1. Logger.LogInfo
  2. Logger.LogError
  3. Logger.LogWarn
  4. Logger.LogDebug
Your logs are ready now, but how can you view logs recorded?

So, now is the time for

SQLiteLoggerViewer

This is the viewer for our logs. To use it follow these steps:
  • Remove any controller with name "Logger".
  • Install SQLiteLoggerViewer Nuget Package.
  • In your configuration file (web.config) please add the following App settings:
    • LiteLogFilePath: Path of Log file that you downloaded with respect to project folder. Make sure to add this key to avoid exceptions. In case you already added this key for the first package you can ignore this step.
      • <add key="LiteLogFilePath" value="<Path-to-log-file>"/>
    • LiteLogLevel: Level of logs you want to be logged. You may choose [All-Info-Warn-Debug-Error]. Make sure to add this key as the logger will not log anything in case this key is missed.In case you already added this key for the first package you can ignore this step.
      • <add key="LiteLogLevel" value="All"/>
    • LiteLogsCount: Number of logs you want to view in one page. If you don't add this key, default value of 20 log will be viewed.
      • <add key="LiteLogsCount" value="<number-of-logs>"/>
    • LiteLogPassword: Password to access logs page. Make sure to add this key as logs page will not be accessible if it is missed.
      • <add key="LiteLogPassword" value="<Password>"/>
Now we still have one final step to view our logs. In your configuration file (web.config), replace this code:

<entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>

with the following code:

<entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>


Now you can view the logs, you shall go to <Application URL>/Logger page of your application. Before you can view the logs you will be redirected to Logger login page. Enter the password that you configured to be redirected to the logs page.

Comments