Logger and exceptionManager
This commit is contained in:
CMS4390\marantalu
2017-12-04 17:23:22 +01:00
parent 706c4763c0
commit e63dc12dff
21 changed files with 3418 additions and 93 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
<!-- CMS Configuration -->
<add key="Url" value="http://localhost:9000/index.html" />
<add key="TranspColor" value="#00ff00" />
<add key="OpenNcHMI" value="true" /><!-- True|False: Open Nc HMI -->
<add key="OpenNcHMI" value="false" /><!-- True|False: Open Nc HMI -->
<add key="NCType" value="0" /><!-- 0: Demo - 1: Fanuc - 2: Siemens - 3: Osai -->
<add key="IDClient" value="1" /><!-- 1.. N Client Identification -->
+9 -9
View File
@@ -9,15 +9,15 @@ namespace Step.Config
{
public static class StartupConfig
{
public static ServerConfigModel serverConfig;
public static NcConfigModel ncConfig;
public static ServerConfigModel ServerConfig;
public static NcConfigModel NcConfig;
public static AreasConfigModel productionConfig;
public static AreasConfigModel toolingConfig;
public static AreasConfigModel reportConfig;
public static AreasConfigModel alarmsConfig;
public static AreasConfigModel maintenanceConfig;
public static AreasConfigModel utilitiesConfig;
public static AreasConfigModel scadaConfig;
public static AreasConfigModel ProductionConfig;
public static AreasConfigModel ToolingConfig;
public static AreasConfigModel ReportConfig;
public static AreasConfigModel AlarmsConfig;
public static AreasConfigModel MaintenanceConfig;
public static AreasConfigModel UtilitiesConfig;
public static AreasConfigModel ScadaConfig;
}
}
+55 -46
View File
@@ -6,49 +6,58 @@ using System.Xml.Linq;
using System.Linq;
using static Step.Config.StartupConfig;
using Step.Model.ConfigModels;
using static Step.Config.Constants;
using static Step.Utils.Constants;
using Step.Utils;
namespace Step.Config
{
public static class StartupConfigController
{
public static void ReadStartupConfig()
{
{
// Read validation file
XmlSchemaSet readerSettings = new XmlSchemaSet();
// Add Schema
readerSettings.Add(null, "startupValidator.xsd");
// Open file reader
XDocument xmlConfigFile = XDocument.Load("startupConfig.xml");
// Validate file
xmlConfigFile.Validate(readerSettings, ValidationHandler);
// Read nc Config with LINQ
ncConfig = xmlConfigFile
.Descendants(NC_CONFIG_KEY)
.Select(x => new NcConfigModel()
{
NcVendor = Convert.ToInt32(x.Element("ncVendor").Value),
NcIpAddress = x.Element("ncIpAddress").Value,
NcPort = Convert.ToInt32(x.Element("ncPort").Value)
}).FirstOrDefault();
try
{
// Add Schema
readerSettings.Add(null, STARTUP_CONFIG_SCHEMA_PATH);
// Open file reader
XDocument xmlConfigFile = XDocument.Load(STARTUP_CONFIG_PATH);
// Validate file
xmlConfigFile.Validate(readerSettings, ValidationHandler);
// Read server config with LINQ and save into static config
serverConfig = xmlConfigFile
.Descendants(SERVER_CONFIG_KEY)
.Select(x => new ServerConfigModel()
{ // Set server config model data
Language = x.Element("language").Value,
ServerPort = Convert.ToInt32(x.Element("serverPort").Value),
EnableDirectoryBrowsing = Convert.ToBoolean(x.Element("enableDirectoryBrowsing").Value)
}).FirstOrDefault();
// Read nc Config with LINQ
NcConfig = xmlConfigFile
.Descendants(NC_CONFIG_KEY)
.Select(x => new NcConfigModel()
{
NcVendor = Convert.ToInt32(x.Element("ncVendor").Value),
NcIpAddress = x.Element("ncIpAddress").Value,
NcPort = Convert.ToInt32(x.Element("ncPort").Value)
}).FirstOrDefault();
// Read areas config with LINQ
xmlConfigFile
.Descendants(AREAS_CONFIG_KEY) // Get areas config node
.Elements()
.ToList()
.ForEach(x => SetAreaValueByName(x)); // Loop through elements
// Read server config with LINQ and save into static config
ServerConfig = xmlConfigFile
.Descendants(SERVER_CONFIG_KEY)
.Select(x => new ServerConfigModel()
{ // Set server config model data
Language = x.Element("language").Value,
ServerPort = Convert.ToInt32(x.Element("serverPort").Value),
EnableDirectoryBrowsing = Convert.ToBoolean(x.Element("enableDirectoryBrowsing").Value)
}).FirstOrDefault();
// Read areas config with LINQ
xmlConfigFile
.Descendants(AREAS_CONFIG_KEY) // Get areas config node
.Elements()
.ToList()
.ForEach(x => SetAreaValueByName(x)); // Loop through elements
} catch (Exception ex)
{
ExceptionManager.Manage(ex);
}
}
private static void SetAreaValueByName(XElement element)
@@ -57,25 +66,25 @@ namespace Step.Config
switch (element.Name.ToString())
{
case AREAS.PRODUCTION_KEY:
SetAreaValue(ref productionConfig, element);
SetAreaValue(ref ProductionConfig, element);
break;
case AREAS.TOOLING_KEY:
SetAreaValue(ref toolingConfig, element);
SetAreaValue(ref ToolingConfig, element);
break;
case AREAS.REPORT_KEY:
SetAreaValue(ref reportConfig, element);
SetAreaValue(ref ReportConfig, element);
break;
case AREAS.ALARMS_KEY:
SetAreaValue(ref alarmsConfig, element);
SetAreaValue(ref AlarmsConfig, element);
break;
case AREAS.MAINTENANCE_KEY:
SetAreaValue(ref maintenanceConfig, element);
SetAreaValue(ref MaintenanceConfig, element);
break;
case AREAS.UTILITIES_KEY:
SetAreaValue(ref utilitiesConfig, element);
SetAreaValue(ref UtilitiesConfig, element);
break;
case AREAS.SCADA_KEY:
SetAreaValue(ref scadaConfig, element);
SetAreaValue(ref ScadaConfig, element);
break;
}
}
@@ -110,19 +119,19 @@ namespace Step.Config
switch (areaName)
{
case AREAS.PRODUCTION_KEY:
return productionConfig.Enabled;
return ProductionConfig.Enabled;
case AREAS.TOOLING_KEY:
return toolingConfig.Enabled;
return ToolingConfig.Enabled;
case AREAS.REPORT_KEY:
return productionConfig.Enabled;
return ProductionConfig.Enabled;
case AREAS.ALARMS_KEY:
return alarmsConfig.Enabled;
return AlarmsConfig.Enabled;
case AREAS.MAINTENANCE_KEY:
return maintenanceConfig.Enabled;
return MaintenanceConfig.Enabled;
case AREAS.UTILITIES_KEY:
return utilitiesConfig.Enabled;
return UtilitiesConfig.Enabled;
case AREAS.SCADA_KEY:
return scadaConfig.Enabled;
return ScadaConfig.Enabled;
default:
return false;
}
+4 -1
View File
@@ -41,7 +41,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StartupConfig.cs" />
<Compile Include="StartupConfigController.cs" />
@@ -57,6 +56,10 @@
<Project>{631375dd-06d3-49bb-8130-d9ddb34c429d}</Project>
<Name>Step.Model</Name>
</ProjectReference>
<ProjectReference Include="..\Step.Utils\Step.Utils.csproj">
<Project>{cbeb631b-abfa-4042-9779-c0060b0dfefe}</Project>
<Name>Step.Utils</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="startupConfig.xml">
@@ -1,5 +1,4 @@

namespace Step.Config
namespace Step.Utils
{
public static class Constants
{
@@ -19,6 +18,7 @@ namespace Step.Config
public const string NC_CONFIG_KEY = "ncConfig";
public const string AREAS_CONFIG_KEY = "areasConfig";
// Step Areas
public static class AREAS
{
public const string PRODUCTION_KEY = "production";
@@ -29,5 +29,9 @@ namespace Step.Config
public const string UTILITIES_KEY = "utilities";
public const string SCADA_KEY = "scada";
}
// Filenames
public const string STARTUP_CONFIG_SCHEMA_PATH = "startupValidator.xsd";
public const string STARTUP_CONFIG_PATH = "startupConfig.xml";
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
namespace Step.Utils
{
public static class ExceptionManager
{
public static void Manage(Exception ex)
{
if (ex is System.IO.FileNotFoundException)
{
// File not found
StepLogger.FatalException(ex);
Environment.Exit(0);
}
else
{
// Default case
StepLogger.FatalException(ex);
}
}
}
}
+42
View File
@@ -0,0 +1,42 @@
<?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="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="logDirectory" value="${basedir}/logs" />
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<target name="logfile" xsi:type="File" fileName="${logDirectory}/${shortdate}.txt"/>
<target name="console" xsi:type="Console" />
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="logfile"/>
<logger name="*" minlevel="Debug" writeTo="console"/>
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
+3103
View File
File diff suppressed because it is too large Load Diff
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Le informazioni generali relative a un assembly sono controllate dal seguente
// set di attributi. Modificare i valori di questi attributi per modificare le informazioni
// associate a un assembly.
[assembly: AssemblyTitle("Step.Utils")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Step.Utils")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Se si imposta ComVisible su false, i tipi in questo assembly non saranno visibili
// ai componenti COM. Se è necessario accedere a un tipo in questo assembly da
// COM, impostare su true l'attributo ComVisible per tale tipo.
[assembly: ComVisible(false)]
// Se il progetto viene esposto a COM, il GUID seguente verrà utilizzato come ID della libreria dei tipi
[assembly: Guid("cbeb631b-abfa-4042-9779-c0060b0dfefe")]
// Le informazioni sulla versione di un assembly sono costituite dai seguenti quattro valori:
//
// Versione principale
// Versione secondaria
// Numero di build
// Revisione
//
// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build
// usando l'asterisco '*' come illustrato di seguito:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+63
View File
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Step.Utils</RootNamespace>
<AssemblyName>Step.Utils</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="ExceptionManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StepLogger.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="NLog.xsd">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
+25
View File
@@ -0,0 +1,25 @@
using System;
using NLog;
namespace Step.Utils
{
public static class StepLogger
{
public static Logger Log = LogManager.GetCurrentClassLogger();
public static void ExceptionError(Exception exception)
{
Log.Error(exception.Message);
}
public static void FatalException(Exception exception)
{
Log.Fatal(exception.Message);
}
public static void Info(string message)
{
Log.Info(message);
}
}
}
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="4.4.12" targetFramework="net461" />
</packages>
+15 -1
View File
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
VisualStudioVersion = 15.0.27004.2009
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step", "Step\Step.csproj", "{AFED34E1-77DB-4D81-830A-A8D0A190573D}"
ProjectSection(ProjectDependencies) = postProject
@@ -22,6 +22,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.Config", "Step.Config\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.Client", "Step.Client\Step.Client.csproj", "{66FA29DB-925A-402B-A4C7-D3D780FB1BC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.Utils", "Step.Utils\Step.Utils.csproj", "{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +106,18 @@ Global
{66FA29DB-925A-402B-A4C7-D3D780FB1BC3}.Release|x64.Build.0 = Release|x64
{66FA29DB-925A-402B-A4C7-D3D780FB1BC3}.Release|x86.ActiveCfg = Release|x86
{66FA29DB-925A-402B-A4C7-D3D780FB1BC3}.Release|x86.Build.0 = Release|x86
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|x64.ActiveCfg = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|x64.Build.0 = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|x86.ActiveCfg = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Debug|x86.Build.0 = Debug|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|Any CPU.Build.0 = Release|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|x64.ActiveCfg = Release|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|x64.Build.0 = Release|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|x86.ActiveCfg = Release|Any CPU
{CBEB631B-ABFA-4042-9779-C0060B0DFEFE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+2 -1
View File
@@ -32,10 +32,11 @@ namespace Step.App_Start
// Register Swagger config
SwaggerConfig.Register(config);
app.UseWebApi(config);
// Configure api authentication
ConfigureWebApiOAuth(app);
app.UseWebApi(config);
// SignalR config & startup
SignalRConfig(app);
+2 -1
View File
@@ -10,7 +10,7 @@ using Microsoft.AspNet.SignalR.Hubs;
using Step.Config;
using Step.Database.Controllers;
using Step.Model;
using static Step.Config.Constants;
using static Step.Utils.Constants;
namespace Step.Attributes
{
@@ -18,6 +18,7 @@ namespace Step.Attributes
{
public string Category;
public ACTIONS Action;
protected override bool UserAuthorized(IPrincipal user)
{
if (!base.UserAuthorized(user))
+1 -1
View File
@@ -5,7 +5,7 @@ using System.Web.Http;
using System.Web.Http.Controllers;
using Step.Database.Controllers;
using Step.Config;
using static Step.Config.Constants;
using static Step.Utils.Constants;
using Step.Model;
namespace Step
-2
View File
@@ -4,8 +4,6 @@ using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Step.Attributes;
using static Step.Config.Constants;
namespace Step.Controllers
{
+1 -2
View File
@@ -1,7 +1,6 @@
using Step.Model;
using System.Web.Http;
using Step.Database.Controllers;
using static Step.Config.Constants;
using static Step.Utils.Constants;
namespace Step.Controllers
{
+1 -1
View File
@@ -7,7 +7,7 @@ using Microsoft.Owin.Security.OAuth;
using Step.Database.Controllers;
using Step.Model;
using System.Security.Claims;
using static Step.Config.Constants;
using static Step.Utils.Constants;
namespace Step.Provider
{
+4
View File
@@ -297,6 +297,10 @@
<Project>{20fc0937-e7ca-4693-95f9-7a948efd173b}</Project>
<Name>Step.UI</Name>
</ProjectReference>
<ProjectReference Include="..\Step.Utils\Step.Utils.csproj">
<Project>{cbeb631b-abfa-4042-9779-c0060b0dfefe}</Project>
<Name>Step.Utils</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{4A0DDDB5-7A95-4FBF-97CC-616D07737A77}" />
+22 -25
View File
@@ -8,8 +8,9 @@ using System.Threading;
using System.Web;
using TeamDev.SDK;
using TeamDev.SDK.MVVM;
using static Step.Config.StartupConfig;
using Step.Config;
using static Step.Config.StartupConfig;
using static Step.Utils.StepLogger;
namespace Step
{
@@ -22,34 +23,30 @@ namespace Step
public static void Main()
{
try
Info("Application started");
StartupConfigController.ReadStartupConfig();
// Start self host application
string configuredUri = "http://localhost:" + ServerConfig.ServerPort.ToString();
// Start WinForm
ServerControlWindow.Start();
// Register listener to "close application" messages
MessageServices.Current.Subscribe("StopServer", (a, b) =>
{
StartupConfigController.ReadStartupConfig();
StopRequest.Set();
});
// Start self host application
string configuredUri = "http://localhost:" + serverConfig.ServerPort.ToString();
// Start WinForm
ServerControlWindow.Start();
// Register listener to "close application" messages
MessageServices.Current.Subscribe("StopServer", (a, b) =>
{
StopRequest.Set();
});
// Start server services
using (WebApp.Start<Step.App_Start.Startup>(url: configuredUri))
{
StopRequest.WaitOne();
}
// Close WinForm
ServerControlWindow.Stop();
}
catch (Exception ex)
// Start server services
using (WebApp.Start<Step.App_Start.Startup>(url: configuredUri))
{
Console.WriteLine(ex);
StopRequest.WaitOne();
Info("Application closed");
}
// Close WinForm
ServerControlWindow.Stop();
}
}
}