Configuring Serilog with Azure Application Insights
This topic contains information related to the configuration of Serilog with Application Insights for Aptify Smart Client (Desktop) and Aptify Web.
Serilog and Application Insights provide facility to log application related information such as errors, warning messages etcetera to text file and to Azure Application Insights. To enable this feature, make the configuration changes as described in below points. For Smart Client (Desktop) application set the values in Aptify Shell.Exe.config file and for Aptify Web application set the value in Web.config file.
Add the publisher tag in exceptionManagement section of Aptify Shell.Exe.config and Web.config as shown below.
<exceptionManagement mode="on" defaultseverity="0"> <publisher mode="on" days="3" assembly="AptifyExceptionManagement" type="Aptify.Framework.ExceptionManagement.SerilogPublisher"/> </exceptionManagement>
Add the below keys in appSettings sections of AptifyShell.Exe.config and Web.config.
<appSettings> <add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" /> <add key="serilog:write-to:RollingFile.pathFormat" value="" /> <add key="serilog:write-to:RollingFile.outputTemplate" value="[{Timestamp:hh:mm:ss}{Level:u3}] [UserCredentials: {UserContext}] [ClassName:{SourceContext}]{Message} {NewLine}{Exception}" /> <add key="serilog:write-to:RollingFile.rollingInterval" value="Day" /> <add key="serilog:write-to:RollingFile.rollOnFileSizeLimit" value="true" /> <add key="serilog:write-to:RollingFile.shared" value="true" /> <add key="serilog:minimum-level" value="information" /> <add key="Aptify.Framework.LoggerFactory.InstrumentationKey" value="" /> <add key="Aptify.Framework.LoggerFactory.isAppInsightsLoggingDisabled" value="true" /> <add key="Aptify.Framework.LoggerFactory.isLoggingDisabled" value="true" /> </appSettings>
To save the logs to a text file, provide the file path as the value to the application setting key serilog:write-to:RollingFile.pathFormat.
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\inetpub\logs\SeriLog\log.txt" />
In case you do not want to the save the logs to text file, comment the serilog:write-to:RollingFile.pathFormat line to avoid the empty path error.
Add the Instrumentation Key value in application setting key Aptify.Framework.LoggerFactory. InstrumentationKey. You will get the instrumentation key on overview page of Azure application insights as shown in the below screenshot.
To enable logging for both Serilog and Application Insights, set the value of keys as shown below
To enable only Serilog logging without Application Insights, set the value of keys as shown below.
To enable only Application Insights logging, set the value of below keys to false and comment out serilog:write-to:RollingFile.pathFormat key.
To check the Application Insights log details:
Go to “Home - Microsoft Azure” and search the Application Insights you used for your logging purpose
Under Application Insights, go to Monitoring => Logs and execute query “traces
| order by timestamp desc “to search the logs. Below screenshot is for reference purpose.