Added logging (Still WIP)

pull/13/head
tidusjar 9 years ago
parent d0469ecf84
commit 46dc9d95f1

@ -33,11 +33,12 @@ namespace PlexRequests.Core
public class Setup
{
public void SetupDb()
public string SetupDb()
{
var db = new DbConfiguration(new SqliteFactory());
db.CheckDb();
TableCreation.CreateTables(db.DbConnection());
return db.DbConnection().ConnectionString;
}
}
}

@ -0,0 +1,41 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: LogEntity.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
namespace PlexRequests.Store.Models
{
public class LogEntity : Entity
{
public string Username { get; set; }
public DateTime Date { get; set; }
public string Level { get; set; }
public string Logger { get; set; }
public string Message { get; set; }
public string Callsite { get; set; }
public string Exception { get; set; }
}
}

@ -41,6 +41,10 @@
<Reference Include="Mono.Data.Sqlite">
<HintPath>..\Assemblies\Mono.Data.Sqlite.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.2.3\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -57,6 +61,7 @@
<Compile Include="ISqliteConfiguration.cs" />
<Compile Include="IRepository.cs" />
<Compile Include="Models\GlobalSettings.cs" />
<Compile Include="Models\LogEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repository\JsonRepository.cs" />
<Compile Include="SettingsModel.cs" />

@ -40,3 +40,16 @@ CREATE TABLE IF NOT EXISTS GlobalSettings
SettingsName varchar(50) NOT NULL,
Content varchar(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS Log
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Username varchar(50) NOT NULL,
Date varchar(100) NOT NULL,
Level varchar(100) NOT NULL,
Logger varchar(100) NOT NULL,
Message varchar(100) NOT NULL,
CallSite varchar(100) NOT NULL,
Exception varchar(100) NOT NULL
);

@ -2,4 +2,5 @@
<packages>
<package id="Dapper" version="1.42" targetFramework="net452" />
<package id="Dapper.Contrib" version="1.42" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net452" />
</packages>

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target name="console" xsi:type="Console" layout="${message}"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>

File diff suppressed because it is too large Load Diff

@ -100,6 +100,10 @@
<HintPath>..\packages\Nancy.Viewengines.Razor.1.4.1\lib\net40\Nancy.ViewEngines.Razor.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.2.3\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
<Private>True</Private>
@ -179,6 +183,12 @@
</Content>
<None Include="app.config" />
<Content Include="packages.config" />
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="NLog.xsd">
<SubType>Designer</SubType>
</None>
<None Include="sqlite3.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

@ -25,11 +25,19 @@
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Owin.Hosting;
using Mono.Data.Sqlite;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Targets;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
@ -45,7 +53,10 @@ namespace PlexRequests.UI
WriteOutVersion();
var s = new Setup();
s.SetupDb();
var connection = s.SetupDb();
//ConfigureTargets(connection);
var uri = GetStartupUri();
@ -76,5 +87,58 @@ namespace PlexRequests.UI
return uri;
}
private static void ConfigureTargets(string connectionString)
{
LogManager.ThrowExceptions = true;
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var databaseTarget = new DatabaseTarget { CommandType = CommandType.Text,ConnectionString = connectionString,
DBProvider = "Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756",
Name = "database"};
var messageParam = new DatabaseParameterInfo { Name = "@Message", Layout = "${message}" };
var callsiteParam = new DatabaseParameterInfo { Name = "@Callsite", Layout = "${callsite}" };
var levelParam = new DatabaseParameterInfo { Name = "@Level", Layout = "${level}" };
var usernameParam = new DatabaseParameterInfo { Name = "@Username", Layout = "${identity}" };
var dateParam = new DatabaseParameterInfo { Name = "@Date", Layout = "${date}" };
var loggerParam = new DatabaseParameterInfo { Name = "@Logger", Layout = "${logger}" };
var exceptionParam = new DatabaseParameterInfo { Name = "@Exception", Layout = "${exception:tostring}" };
databaseTarget.Parameters.Add(messageParam);
databaseTarget.Parameters.Add(callsiteParam);
databaseTarget.Parameters.Add(levelParam);
databaseTarget.Parameters.Add(usernameParam);
databaseTarget.Parameters.Add(dateParam);
databaseTarget.Parameters.Add(loggerParam);
databaseTarget.Parameters.Add(exceptionParam);
databaseTarget.CommandText = "INSERT INTO Log (Username,Date,Level,Logger, Message, Callsite, Exception) VALUES(@Username,@Date,@Level,@Logger, @Message, @Callsite, @Exception);";
config.AddTarget("database", databaseTarget);
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Error, databaseTarget);
config.LoggingRules.Add(rule1);
try
{
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
catch (Exception e)
{
throw;
}
// Example usage
Logger logger = LogManager.GetLogger("Example");
logger.Error("error log message");
}
}
}

@ -11,6 +11,9 @@
<package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net452" />
<package id="Nancy.Owin" version="1.4.1" targetFramework="net452" />
<package id="Nancy.Viewengines.Razor" version="1.4.1" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net452" />
<package id="NLog.Config" version="4.2.3" targetFramework="net452" />
<package id="NLog.Schema" version="4.0.0" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" />
<package id="System.Collections" version="4.0.0" targetFramework="net452" />

Loading…
Cancel
Save