LogWriterHelper.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 
7 namespace Sage.ES.S50.CloudId
8 {
9  public static class LogWriterHelper_Obsolete
10  {
11  #region Debugging
12  //Ruta nueva. Aqui deberian poderse escribir trazas
13  private static string debugFileName1 = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "SageCloudId.log");
14  //Ruta original. Dejamos por compatibilidad si no se puede escribir en la ruta1
15  private static string debugFileName2 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SageCloudId.log");
16 
17  private static string debugFilenameDefinitive = debugFileName1; //Por defecto usamos la nueva ruta para que el NetworkService pueda escribir
18 #if DEBUG
19  private static int _logStatus = 1;
20 #else
21  private static int _logStatus = -1;
22 #endif
23 
24  private static int _retryAttempts = 3;
25  private static int _retryIntervalMs = 100;
26 
27  private static bool writeLog
28  {
29  get
30  {
31  if (_logStatus == -1)
32  {
33  if (System.IO.File.Exists(debugFileName1))
34  {
35  debugFilenameDefinitive = debugFileName1;
36  _logStatus = 1;
37  }
38  else if (System.IO.File.Exists(debugFileName2))
39  {
40  debugFilenameDefinitive = debugFileName2;
41  _logStatus = 1;
42  }
43  else
44  _logStatus = 0;
45  }
46  return _logStatus == 1;
47  }
48  }
49 
50  public static void WriteDebugLine(string debugText)
51  {
52  if (writeLog)
53  {
54  bool success;
55  int failedTries = 0;
56  string cadena2Write = DateTime.Now.ToString() + " -> " + debugText + Environment.NewLine;
57  do
58  {
59  try
60  {
61  System.IO.File.AppendAllText(debugFilenameDefinitive, cadena2Write);
62  success = true;
63  }
64  catch
65  {
66  failedTries++;
67  success = false;
68  if (failedTries < _retryAttempts)
69  System.Threading.Thread.Sleep(_retryIntervalMs);
70  else
71  {
72  _logStatus = 0;
73  //Para informar de esto, intentamos crear una meta-traza
74  string metaError = "Se ha producido un error no recuperable al generar trazas de Overdrive por el Sage50cEs App Adapter." + Environment.NewLine + " - Archivo: " + debugFilenameDefinitive + Environment.NewLine + " - Último mensaje: " + cadena2Write;
75  try
76  {
77  System.IO.File.AppendAllText(debugFilenameDefinitive + ".meta", metaError);
78  }
79  catch
80  {
81  //Si la meta-traza tampoco se puede guardar, usamos el event log
82  try
83  {
84  using (System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("Application"))
85  {
86  eventLog.Source = "Sage CloudID Exe Helper App";
87  eventLog.WriteEntry(metaError, System.Diagnostics.EventLogEntryType.Warning);
88  }
89  }
90  catch
91  {
92  //Si esto ya no funciona, nos tiramos por un puente... :(
93  }
94  }
95  }
96  }
97  } while (!success && failedTries < _retryAttempts);
98  }
99  }
100 
101  public static void ResetLogStatus()
102  {
103  _logStatus = -1;
104  }
105 
106  public static string ProcessException(Exception ex)
107  {
108  string exceptionData = "**** Datos de excepción no controlada ****" + Environment.NewLine;
109  exceptionData += " Mensaje: " + ex.Message + Environment.NewLine;
110  exceptionData += " Origen: " + ex.Source + Environment.NewLine;
111  exceptionData += " Pila: " + ex.StackTrace + Environment.NewLine;
112  if (ex is AggregateException aex)
113  {
114  aex = aex.Flatten();
115 
116  for (int index = 0; index < aex.InnerExceptions.Count; index++)
117  {
118  Exception exI = aex.InnerExceptions[index];
119  exceptionData += " Mensaje excepcion anidada " + (index + 1).ToString() + " : " + exI.Message + Environment.NewLine;
120  exceptionData += " Origen excepcion anidada " + (index + 1).ToString() + " : " + exI.Source + Environment.NewLine;
121  exceptionData += " Pila excepcion anidada " + (index + 1).ToString() + " : " + exI.StackTrace + Environment.NewLine;
122 
123  if (exI.InnerException != null)
124  {
125  exceptionData += " Mensaje excepcion anidada " + (index + 1).ToString() + " Inner : " + exI.InnerException.Message + Environment.NewLine;
126  exceptionData += " Origen excepcion anidada " + (index + 1).ToString() + " Inner : " + exI.InnerException.Source + Environment.NewLine;
127  exceptionData += " Pila excepcion anidada " + (index + 1).ToString() + " Inner : " + exI.InnerException.StackTrace + Environment.NewLine;
128  }
129 
130  }
131  }
132  else if (ex.InnerException != null)
133  {
134  exceptionData += " Mensaje Inner Exception: " + ex.InnerException.Message + Environment.NewLine;
135  exceptionData += " Origen Inner Exception: " + ex.InnerException.Source + Environment.NewLine;
136  exceptionData += " Pila Inner Exception: " + ex.InnerException.StackTrace + Environment.NewLine;
137  }
138 
139  return exceptionData;
140  }
141  #endregion
142  }
143 }