<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    <section name="UaApplicationConfiguration" type="UnifiedAutomation.UaBase.ApplicationConfigurationSection,UnifiedAutomation.UaBase"/>
  </configSections>
  <nlog autoReload="true" throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="logDirectory" value="${basedir}/../../logs"/>
    <targets>
      <target name="console" xsi:type="ColoredConsole" layout="${longdate} | ${level:uppercase=true} | ${logger} :: ${message}"/>

      <target name="allLogsFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/RSProduction.log" archiveFileName="${logDirectory}/archive/RSProduction.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>
      <target name="mqttLogsFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/Mqtt.log" archiveFileName="${logDirectory}/archive/Mqtt.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>
		
		<target name="HttpLogsFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
			<target xsi:type="File" fileName="${logDirectory}/HttpAPI.log" archiveFileName="${logDirectory}/archive/HttpAPI.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
		</target>
      <target name="stopVerificationFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
			<target xsi:type="File" fileName="${logDirectory}/StopVerification.log" archiveFileName="${logDirectory}/archive/StopVerification.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
		</target>


		<target name="connectionErrorsFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/connection-errors.log" archiveFileName="${logDirectory}/archive/connection-errors.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>

      <target name="seriousLogsFile" xsi:type="File" fileName="${logDirectory}/RSProduction-errors.log" archiveFileName="${logDirectory}/archive/RSProduction-errors.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="50" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>

      <target name="nlogConf" xsi:type="File" fileName="${logDirectory}/NLog.conf.log" archiveFileName="${logDirectory}/archive/NLog.conf.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="5" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>


      <target name="opcClientLogFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/opc-client.log" archiveFileName="${logDirectory}/archive/opc-client.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>

      <target name="linkLogFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/mobile-link.log" archiveFileName="${logDirectory}/archive/mobile-link.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>
		
	  <target name="reportRenderLogFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
	  	<target xsi:type="File" fileName="${logDirectory}/report-render.log" archiveFileName="${logDirectory}/archive/report-render.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}" />
	  </target>
		
      <target name="modificationsFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/modifications.log" archiveFileName="${logDirectory}/archive/modifications.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate}|${level:uppercase=true}|${assembly-version}|${logger} :: ${message}"/>
      </target>

      <target name="seriousLogsInfluxDB" xsi:type="InfluxDB"></target>

      <target name="auditLogFile" xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
        <target xsi:type="File" fileName="${logDirectory}/audit.log" archiveFileName="${logDirectory}/archive/audit.{#}.log" archiveNumbering="DateAndSequence" archiveEvery="Day" archiveDateFormat="yyyyMMdd" maxArchiveFiles="14" layout="${longdate} ${message}"/>
      </target>

		<target xsi:type="FallbackGroup" name="logstash" returnToFirstOnSuccess="true">
			<target xsi:type="AsyncWrapper">
				<target xsi:type="Network" address="tcp://logs.rsproduction.se" newLine="true">
					<layout xsi:type="JsonLayout" includeAllProperties="false">
						<attribute name="@timestamp" layout="${date:universalTime=true:format=yyyy-MM-ddTHH\:mm\:ss.fffZ}"/>
						<attribute name="versionNumber" layout="${assembly-version}"/>
						<attribute name="installationID" layout="${gdc:item=installationIDLog}"/>
						<attribute name="level" layout="${level:upperCase=true}"/>
						<attribute name="category" layout="${logger}"/>
						<attribute name="message" layout="${message}"/>
						<attribute name="environment" layout="production"/>
						<attribute name="extra" layout="value"/>
					</layout>
				</target>
			</target>
			<target xsi:type="AsyncWrapper" queueLimit="10000" timeToSleepBetweenBatches="100" overflowAction="Grow">
				<target xsi:type="File" fileName="${logDirectory}/fallBackLogstash.log" archiveFileName="${logDirectory}/archive/fallBackLogstash.{#}.log" archiveNumbering="DateAndSequence" archiveAboveSize="10485760" archiveDateFormat="yyyyMMdd" maxArchiveFiles="10" layout="${longdate} ${message}"/>
			</target>
		</target>
    </targets>

    <rules>


      <!-- filter all connection issues spam to seperate file -->
      <logger name="NLog.config" minlevel="Trace" writeTo="nlogConf" final="true"/>
      <logger name="SendCheckInMessageToHubs" minlevel="Error" writeTo="connectionErrorsFile" final="true"/>
      <logger name="SendGetServerTimeMessageToHubs" minlevel="Error" writeTo="connectionErrorsFile" final="true"/>
      <logger name="SendNewChangesToHubs" minlevel="Warn" writeTo="connectionErrorsFile" final="true"/>
      <logger name="auditLogs" minlevel="Info" writeTo="auditLogFile" final="true"/>

        <logger name="OPC*" minlevel="Info" writeTo="opcClientLogFile" final="true" />
		<logger name="Link*" minlevel="Info" writeTo="linkLogFile" final="true" />
		<logger name="ReportRenderService*" minlevel="Info" writeTo="reportRenderLogFile" final="true" />
        <logger name="HttpAPI" minlevel="Info" writeTo="HttpLogsFile" final="true" />
      <logger name="StopVerificationService" minlevel="Info" writeTo="stopVerificationFile" final="true" />
        <logger name="MQTT*" minlevel="Info" writeTo="MqttLogsFile" final="true" />
        <logger name="Modifications*" minlevel="Info" writeTo="modificationsFile" final="true" />

      <!-- actual logs: change minlevel="Info" to minlevel="Trace" for full debugging -->
      <logger name="*" minlevel="Info" writeTo="logstash"/>
      <logger name="*" minlevel="Info" writeTo="allLogsFile"/>

        <logger name="*" minlevel="Error" writeTo="seriousLogsFile"/>
      
        <!-- Uncomment below to enable Influx-Logging  -->
        <!-- <logger name="*" minlevel="Warn" writeTo="seriousLogsInfluxDB" /> -->


      <logger name="*" minlevel="Info" writeTo="console"/>
    </rules>
  </nlog>
  <UaApplicationConfiguration>
    <SecuredApplication xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://opcfoundation.org/UA/2011/03/SecuredApplication.xsd">

      <!-- Identify the Application -->
      <ApplicationName>RSProduction</ApplicationName>
      <ApplicationUri>urn:localhost:GoodSolutions:UAClient</ApplicationUri>
      <ApplicationType>Client_1</ApplicationType>
      <ProductName>GoodSolutions UAClient</ProductName>

      <!-- Specify location of Certificates and Trust Lists -->
      <ApplicationCertificate>
        <StoreType>Directory</StoreType>
        <StorePath>%CommonApplicationData%\unifiedautomation\UaSdkNetBundleEval\pkiclient\own</StorePath>
        <SubjectName>CN=UAClient/O=GoodSolutions/DC=localhost</SubjectName>
        <ValidationOptions>0</ValidationOptions>
      </ApplicationCertificate>
      <TrustedCertificateStore>
        <StoreType>Directory</StoreType>
        <StorePath>%CommonApplicationData%\unifiedautomation\UaSdkNetBundleEval\pkiclient\trusted</StorePath>
        <ValidationOptions>0</ValidationOptions>
      </TrustedCertificateStore>
      <IssuerCertificateStore>
        <StoreType>Directory</StoreType>
        <StorePath>%CommonApplicationData%\unifiedautomation\UaSdkNetBundleEval\pkiclient\issuers</StorePath>
        <ValidationOptions>0</ValidationOptions>
      </IssuerCertificateStore>
      <RejectedCertificatesStore>
        <StoreType>Directory</StoreType>
        <StorePath>%CommonApplicationData%\unifiedautomation\UaSdkNetBundleEval\pkiclient\rejected</StorePath>
        <ValidationOptions>0</ValidationOptions>
      </RejectedCertificatesStore>

      <Extensions>

        <!-- Specify the Trace settings for the Application -->
        <Extension>
          <TraceSettings xmlns="http://unifiedautomation.com/schemas/2011/12/Application.xsd" MasterTraceEnabled="false" DefaultTraceLevel="Info">
            <TraceFile>%CommonApplicationData%\unifiedautomation\logs\UaSdkNetBundleEval\UAClient.log.txt</TraceFile>
            <MaxEntriesPerLog>100000</MaxEntriesPerLog>
            <MaxLogFileBackups>3</MaxLogFileBackups>
            <FastTrace>false</FastTrace>
            <ModuleSettings>
              <ModuleTraceSettings ModuleName="UnifiedAutomation.Stack"/>
              <ModuleTraceSettings ModuleName="UnifiedAutomation.Client"/>
            </ModuleSettings>
          </TraceSettings>
        </Extension>

        <Extension>
          <ClientSettings xmlns="http://unifiedautomation.com/schemas/2011/12/Application.xsd">
            <ProductUri>urn:GoodSolutions:UAClient</ProductUri>
          </ClientSettings>
          <SubscriptionSettings xmlns="http://unifiedautomation.com/schemas/2011/12/Application.xsd">
            <MaxSubscriptionCount>1000</MaxSubscriptionCount>
          </SubscriptionSettings>
        </Extension>

        <!-- Specify Settings when EXE is run with the /install argument -->
        <Extension>
          <InstallationSettings xmlns="http://unifiedautomation.com/schemas/2011/12/Application.xsd">
            <GenerateCertificateIfNone>true</GenerateCertificateIfNone>
            <!-- Supported values are 1024, 2048, 3072 and 4096. Default is 2048. -->
            <CertificateKeyLength>2048</CertificateKeyLength>
            <!-- Possible values are sha1 and sha256. Default is sha256. -->
            <CertificateHashAlgorithm>sha256</CertificateHashAlgorithm>
            <DeleteCertificateOnUninstall>true</DeleteCertificateOnUninstall>
          </InstallationSettings>
        </Extension>

      </Extensions>

    </SecuredApplication>
  </UaApplicationConfiguration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
  </startup>
  <runtime>
    <!--gcServer enabled="true"/-->
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="Resources"/>
      <dependentAssembly> <!-- Updated to support version 13.0.0.0 required by System.IdentityModel.Tokens.Jwt 5.7.0 -->
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91"/>
        <bindingRedirect oldVersion="1.0.0.0-11.0.0.0" newVersion="10.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.2.0.246" newVersion="1.2.0.246"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
