@Skydiver and @Sheepings thank you again for your help. As you suggested, I decided to log into a file rather than DB. I used Nlog, would you please have a look at my code? Is it good enough?
I already have UnhandledExceptionHandler so I added NLog logger as follows. I removed all the try/catchs in order to manage logging from a single point.
Here is the sample NLog config:
I already have UnhandledExceptionHandler so I added NLog logger as follows. I removed all the try/catchs in order to manage logging from a single point.
C#:
using System;
namespace Game.Handlers
{
public class UnhandledExceptionLogger : ExceptionLogger
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public override void Log(ExceptionLoggerContext context)
{
var ex = context.Exception;
string strLogText = FormatException(ex);
var requestedURi = (string)context.Request.RequestUri.AbsoluteUri;
var requestMethod = context.Request.Method.ToString();
var timeUtc = DateTime.Now;
SqlErrorLogging sqlErrorLogging = new SqlErrorLogging();
ApiError apiError = new ApiError()
{
Message = strLogText,
RequestUri = requestedURi,
RequestMethod = requestMethod,
TimeUtc = DateTime.Now
};
//sqlErrorLogging.InsertErrorLog(apiError);
//NLOG
logger.Error(strLogText + Environment.NewLine + DateTime.Now);
}
private string FormatException(Exception ex, int depth = 0)
{
var indent = "";
if (depth > 0)
indent.PadRight(depth * 4);
var sb = new StringBuilder();
IndentLine($"Source --- {ex.Source}");
IndentLine($"StackTrace --- {ex.StackTrace}");
IndentLine($"TargetSite --- {ex.TargetSite}");
if (ex.InnerException != null)
{
IndentLine("--- Inner Exception ---");
sb.Append(FormatException(ex.InnerException, depth + 1));
IndentLine("--- End Inner Exception ---");
}
if (ex.Message != null)
{
IndentLine($"Message --- {ex.Message}");
}
return sb.ToString();
void IndentLine(string s)
{
sb.Append(indent);
sb.AppendLine(s);
}
}
}
}
Here is the sample NLog config:
C#:
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/MyLogs/${date:format=yyyy-MM-dd}-api.log" />
<target name="eventlog" xsi:type="EventLog" layout="${message}" log="Application" source=" GamePIN Api Services" />
<target name="database" type="Database" connectionString="Data Source=(localdb)\MSSQLLocalDB;Database=Game; Initial Catalog=GameAPI;MultipleActiveResultSets=True;">
<commandText> insert into ExceptionLog ([TimeStamp],[Level],Logger, [Message], UserId, Exception, StackTrace) values (@TimeStamp, @Level, @Logger, @Message, case when len(@UserID) = 0 then null else @UserId end, @Exception, @StackTrace); </commandText>
<parameter name="@TimeStamp" layout="${date}" />
<parameter name="@Level" layout="${level}" />
<parameter name="@Logger" layout="${logger}" />
<parameter name="@Message" layout="${message}" />
<parameter name="@UserId" layout="${mdc:user_id}" />
<parameter name="@Exception" layout="${exception}" />
<parameter name="@StackTrace" layout="${stacktrace}" />
<dbProvider>System.Data.SqlClient</dbProvider>
</target>
</targets>
<rules>
<!-- I am adding my 3 logging rules here -->
<logger name="*" minlevel="Error" writeTo="database" />
<logger name="*" minlevel="Error" writeTo="logfile" />
<logger name="*" minlevel="Error" writeTo="eventlog" />
</rules>
</nlog>
Last edited: