public class ClassLog { protected string _logFilePath = null; public readonly static ClassLog Log = new ClassLog(); public static string LogFilePath { get { return Log._logFilePath; } set { Log._logFilePath = value; } } public virtual void Add(string textToAdd) { if (LogFilePath == null) throw new NullReferenceException("Le nom du fichier log n'a pas été initialisé"); //Ajoute au fichier Console.WriteLine(string.Format("Ajoute [{0}] dans le fichier {1}", textToAdd, _logFilePath)); } } public class ClassLog2 : ClassLog { public readonly static new ClassLog2 Log = new ClassLog2(); public static new string LogFilePath { get { return Log._logFilePath; } set { Log._logFilePath = value; } } public void Add(DateTime date, Exception ex) { if (LogFilePath == null) { ClassLog.Log.Add(" ClassLog2 : Le nom du fichier log n'a pas été initialisé"); //On le repasse à la classe mère... ClassLog.Log.Add(string.Format(" ==> {0} {1}", date.ToString(), ex.Message)); return; } //Ajoute au fichier Console.WriteLine(string.Format("Ajoute [{0} : {1}] dans le fichier {2}", date.ToString(), ex.Message, _logFilePath)); } }
static void Main(string[] args) { ClassLog.LogFilePath = "MonLog.log"; ClassLog2.LogFilePath = "MonLog2.log"; ClassLog.Log.Add("test pour ClassLog"); ClassLog2.Log.Add("test pour ClassLog 2"); ClassLog2.Log.Add(DateTime.Now, new InvalidProgramException("test pour ClassLog2")); }
public sealed class Log { #region Déclarations et propriétés private string logFilePath; /// <summary> /// Path to save the file /// </summary> public string LogFilePath { get { return logFilePath; } set { logFilePath = value; } } //Contructeur public Log() { } public void Add(object stringToAdd) { try { if (LogFilePath != "") { //verify if the directory and file exist FileInfo file = new FileInfo(LogFilePath); if (!file.Directory.Exists) file.Directory.Create(); StreamWriter stream = null; if (!file.Exists) stream = file.CreateText(); else stream = file.AppendText(); try { stream.Write(stringToAdd.ToString()); // empty line; } catch (Exception) { } finally { try { stream.Close(); } catch (Exception) { } finally { } } } } catch (Exception ex) { throw ex; } } #endregion //Fonctions Publiques
public class ExceptionLog { protected static Log log = new Log(); public static string LogFilePath { get { return log.LogFilePath; } set { log.LogFilePath = value; } } public static void Add(Exception ex, bool exceptionAllreadyManaged = false) { StringBuilder exceptionString = new StringBuilder(); exceptionString.AppendLine(); // empty line; try { exceptionString.AppendLine(System.Windows.Forms.Application.ProductName + " Version : " + System.Windows.Forms.Application.ProductVersion); } catch (Exception) { exceptionString.AppendLine("Exception by getting production name and version"); } if (exceptionAllreadyManaged) exceptionString.Append("Erreur gérée (ajout pour suivi : "); else exceptionString.Append("Erreur non gérée : "); exceptionString.AppendLine("Exception : " + ex.ToString()); exceptionString.AppendLine(DateTime.Now.ToString()); exceptionString.AppendLine(Utils.StringFromException(ex)); log.Add(exceptionString.ToString()); #if DEBUG if (!exceptionAllreadyManaged) Utils.ReportError(ex); #endif } }
class LogForData { protected static Log log = new Log(); public static string LogFilePath { get { return log.LogFilePath; } set { log.LogFilePath = value; } } private static bool headerAdded = false; private static void addData(DateTime date, string data1) { StringBuilder strBuidler = new StringBuilder(); strBuidler.Append(date.ToString("dd/MM/yy HH:mm:ss")); strBuidler.Append(";"); strBuidler.Append(data1); strBuidler.AppendLine(); log.Add(strBuidler.ToString()); } public static void Add(DateTime date, string data1) { // Add Header if (!headerAdded) { log.Add("Date; Data1 \r\n"); headerAdded = true; } addData( date, data1); }
public Class Log1 { static string logFilePath = ""; public static string LogFilePath { get { return Log.logFilePath; } set { Log.logFilePath = value; } } public static void Add(string textToAdd) { //ajout dans le fichier } } public Class Log2 : Log1 { public static void Add(DateTime date, Exception ex) { //ajout dans le fichier } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionLog1 comme une pseudo-factory, associée à une enum répertoriant les différents type de log/manière de logger
tu peux aussi utiliser un Singleton.
public class ClsMere { private ClsMere() { } public static ClsMere GetSingleton() { Return MonSingleton; } } public class ClsEnfant:ClsMere { public static ClsEnfant GetSingleton() { Return MonSingletonEnfant; } }