/// <summary> /// Trace response of action /// </summary> /// <param name="response">Response trace</param> protected virtual void TraceResponse(ResponseTrace response) { //Eseguo la stringhificazione della response string value = TraceUtils.StringifyResponse(response); //Di base eseguo il tracciamento sul tracer impostato Debug.Write($"[{GetType().Name}] RESPONSE: {value}"); }
/// <summary> /// Trace response of action /// </summary> /// <param name="response">Response trace</param> protected override void TraceResponse(ResponseTrace response) { //Validazione argomenti if (response == null) { throw new ArgumentNullException(nameof(response)); } //Eseguo la generazione del file di log dell'eventuale errore TraceUtils.WriteErrorLogFile(response, TargetFolder); }
/// <summary> /// Writes an error log file on file system /// </summary> /// <param name="response">Response</param> /// <param name="targetFolder">Target folder</param> public static void WriteErrorLogFile(ResponseTrace response, string targetFolder) { //Validazione argomenti if (response == null) { throw new ArgumentNullException(nameof(response)); } //Se non ho errori, esco dalla funzione if (!response.Error.HasError) { return; } //Se la folder è nulla o vuota, uso come root quella di esecuzione if (string.IsNullOrEmpty(targetFolder)) { targetFolder = AppDomain.CurrentDomain.BaseDirectory; } //Se il la folder non è "rooted", combino con il percorso base if (!Path.IsPathRooted(targetFolder)) { targetFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, targetFolder); } //Compongo il percorso della root folder (la creo se non esiste) if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } //Creo la folder del giorno corrente se non esiste string dayFolder = Path.Combine(targetFolder, DateTime.Now.ToString("yyyy-MM-dd")); if (!Directory.Exists(dayFolder)) { Directory.CreateDirectory(dayFolder); } //Compongo il nome del file con l'eccezione string errorFile = Path.Combine(dayFolder, $"Error_{DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss")}_{DateTime.Now.Millisecond.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0')}.log"); //Il contenuto è un elenco di stringhe con tutti i dettagli IList <string> content = new List <string>(); content.Add($"REQUEST : {StringifyRequest(response.Request)}"); content.Add($"RESPONSE : {StringifyResponse(response)}"); //Scrivo il contenuto sul file File.WriteAllLines(errorFile, content); }
/// <summary> /// Convert to string specified response trace /// </summary> /// <param name="response">Response trace</param> /// <returns>Returns string representing object</returns> public static string StringifyResponse(ResponseTrace response) { //Validazione argomenti if (response == null) { throw new ArgumentNullException(nameof(response)); } //Se il risultato non è nullo, tento la serializzazione solo se non è già una stringa string resultValue = string.IsNullOrEmpty(response.BodyContent) ? "-" : response.BodyContent; //Eseguo la serializzazione della struttura di errore string result = string.IsNullOrEmpty(response.BodyType) ? "null" : $"{response.BodyType}:{resultValue}"; string error = StringifyError(response.Error); //Formatto i valori della request return ($"(uid: {response.Request.UniqueId}) duration:{Math.Round(response.Duration.TotalMilliseconds, 0)}ms - {result} [{error}]"); }
/// <summary> /// Occurs after the action method is invoked. /// </summary> /// <param name="actionExecutedContext">The action executed context.</param> public override void OnActionExecuted(ActionExecutedContext actionExecutedContext) { //Validazione argomenti if (actionExecutedContext == null) { throw new ArgumentNullException(nameof(actionExecutedContext)); } //Leggo il content e il relativo type BodyContent body = GetResponseBody(actionExecutedContext.Result); //Inizializzo la response _Response = TraceUtils.GenerateResponse(_Request, body.Value, body.Type, body.Length, actionExecutedContext.Exception); //Traccio il response (se richiesto) if (EnableResponseTrace) { TraceResponse(_Response); } //Esecuzione delle funzioni base base.OnActionExecuted(actionExecutedContext); }