AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Logger

Ein Thema von Speedmaster · begonnen am 18. Jul 2005
Antwort Antwort
Benutzerbild von Speedmaster
Speedmaster

Registriert seit: 4. Mär 2005
Ort: Karlsruhe
535 Beiträge
 
Delphi 2005 Personal
 
#1

Logger

  Alt 18. Jul 2005, 18:35
Dieser Code wurde zum Zweck erstellt eine das loggen von bestimmten Ereignissen zu vereinfachen, und könnte theoretisch dazu benutzt werden Fehler zu finden wenn der Kunde ein Problem hat!

Folgende Funktionen sind vorhanden:
  • CLog( string Name ) - Konstruktor der Klasse
  • void AddLine( string Line, bool AddTime) - Fügt einen Eintrag mit oder ohne Zeit hinzu
  • void AddSpace( char Space, byte lenght ) - Fügt eine Zeile mit der länge und dem Angegebenen Zeichen hinzu
  • void SaveLog() - Speichert die Log unter dem aktuellen Pfad
  • void SaveLog( string Path ) - Speichert die Log unter dem Pfad der als Parameter angegeben ist
Features:
  • Unterstützt 12(!) Speichermethoden
  • Unterstützt eine Unbegrenzte Anzahl an Logdateien welche zur selben Zeit benutzt werden können
  • Unterstützt Header
  • Unterstützt verschiedene Methoden zum schreiben in die Logdatei
  • Unterstützt Zeit und Datumsangaben

Code:
/***************************************************************
 * Name: uLog                                                 *
 * Date: 08.08.2005                                            *   
 * Time: 18:56                                                 *   
 *************************************************************** 
 *   Copyright © 2005 by Felix Klakow  ( [email]F.Klakow@gmx.de[/email] )   *
 *   All Rights Reserved                                      *
 ***************************************************************/

[color=#0000ff]using[/color] System;
[color=#0000ff]using[/color] System.IO;
[color=#0000ff]using[/color] System.Collections;
[color=#0000ff]using[/color] System.Collections.Specialized;

[color=#0000ff]namespace[/color] LogTest.Log
{
   [color=#0000ff]public struct[/color] SessionSet
   {
      [color=#0000ff]public[/color] string pName;
      // Name der Logdatei
      [color=#0000ff]public[/color] StringCollection HeaderFormat;
      // Format des Headers
      
      [color=#0000ff]public[/color] bool  fsPrint;
      // Gibt an ob eine Consolenausgabe Stattfinden soll      
      [color=#0000ff]public[/color] bool  AddHeader;
      // Gibt an ob ein Header hinzugefügt werden soll
   }

   [color=#0000ff]public class[/color] Session
   {
   [color=#00ff00]      /*
      KLASSENVARIABLEN
      */[/color]

      [color=#0000ff]public[/color] bool Active;
      [color=#0000ff]public[/color] int LeakCount = 0;
      [color=#0000ff]public[/color] string Name;
      
      [color=#0000ff]private[/color] bool Print;
      [color=#0000ff]private[/color] StringCollection Lines;
      

   [color=#00ff00]      /*
      FUNKTIONEN & KONSTRUKTOR
      */[/color]

      [color=#0000ff]public[/color] Session( SessionSet Settings )
      {
         this.Active = true;
         // Setzt Session Aktiv
         this.Print = Settings.fsPrint;
         // Gibt an ob der Inhalt in der Console ausgegeben werden soll
         this.Lines = new StringCollection();
         // Erzeugt ein Stringarray

[color=#00ff00]            /*
          * Der Folgende Code Fügt gegebenfalls einen Header hinzu
          * dazu werden Schlüsselwörter ersetzt.
          */[/color]
         if ( Settings.AddHeader == true )
         {
            int i = 0;
            for (i = 0;i < Settings.HeaderFormat.Count;i++)
            {
               Settings.HeaderFormat[i] = Settings.HeaderFormat[i].Replace("{TIME}",
                                     System.DateTime.Now.ToShortTimeString());
               Settings.HeaderFormat[i] = Settings.HeaderFormat[i].Replace("{DATE}",
                                     System.DateTime.Now.ToShortDateString());
               Settings.HeaderFormat[i] = Settings.HeaderFormat[i].Replace("{USER}",
                                     System.Environment.UserName);
               Settings.HeaderFormat[i] = Settings.HeaderFormat[i].Replace("{LOGNAME}",
                                     Settings.pName);
               this.Lines.Add(Settings.HeaderFormat[i]);
               if ( this.Print == true )
                  Console.WriteLine(this.Lines[i]);
            }
         }
      }

      [color=#0000ff]public[/color] void WriteLine( string Line, bool aTime )
      {
[color=#00ff00]         /*
         Diese Funktion fügt eine neue Zeile mit, oder ohne Zeit hinzu.
         Wenn der Printmodus Aktiviert ist wird der Text in der Console
         ausgegeben.
         */[/color]
         if ( this.Active == false )
         {
            ++LeakCount;

         }
         else
         {
            int lLine;
            if ( aTime == true )
            {
               lLine = this.Lines.Add(
               "[" +
               System.DateTime.Now.ToShortDateString()  + ":" +
               System.DateTime.Now.ToShortTimeString() + "] " + Line);
            }   
            else lLine = this.Lines.Add(Line);
            if ( this.Print == true )
               Console.WriteLine(Lines[lLine]);
         }

      }

      [color=#0000ff]public[/color] void AddSpace( char Space, byte lenght )
      {
[color=#00ff00]         /*
         Diese Funktion fügt eine neue Zeile hinzu die als Trennungszeichen
         zählt. Das Trennungszeichen und die Länge können dabei als Parameter
         bestimmt werden.
         Falls der Printmodus Aktiv ist wird das Trennungszeichen in der
         Console ausgegeben.
         */[/color]
         if ( this.Active == false )
         {
            ++LeakCount;
         }
         else
         {
            int lLine;
            lLine = this.Lines.Add(new string(Space,lenght));
            if ( this.Print == true )
               Console.WriteLine(Lines[lLine]);
         }
      }

      [color=#0000ff]public[/color] bool CloseLog()
      {
[color=#00ff00]         /*
         Fügt die Abschließende Zeile hinzu, und gibt die Log zurück
         setzt Active auf false
         */[/color]
         if ( this.Active == false )
         {
            ++LeakCount;
            return false;            
         }
         else
         {
         this.Active = false;
         return true;
         }
      }
      
      [color=#0000ff]public[/color] StringCollection GetLog()
      {
[color=#00ff00]         /*
          * Gibt den Inhalt der Logdatei zurück.
          */[/color]
         return Lines;      
      }
   }

   [Serializable()]
   [color=#0000ff]public class[/color] SessionManager : CollectionBase
   {
      
      [color=#0000ff]public[/color] SessionManager()
      {
      }
      
      [color=#0000ff]public[/color] Session this[int index]
      {
         get {
            return ((Session)(List[index]));
         }
         set {
            List[index] = value;
         }
      }
      
      [color=#0000ff]public[/color] int Add(SessionSet Parms)
      {
         Session val = new Session(Parms);
         return List.Add(val);
      }
   }

   [color=#0000ff]public class[/color] LogManager
   {
      [color=#0000ff]private[/color] SessionManager Sessions;
      [color=#0000ff]private[/color] int ActiveLog = 0;
         
      [color=#0000ff]public[/color] LogManager( SessionSet MainSet )
      {
         Sessions = new SessionManager();
         Sessions.Add(MainSet);
      }
      
      [color=#0000ff]public[/color] int AddLog( SessionSet nLogSet )
      {
         return Sessions.Add(nLogSet);
      }
      
      [color=#0000ff]public[/color] bool WriteLine( string Line, bool addTime, int ID )
      {
         if (!( Sessions[ID] == null ))
         {
            Sessions[ID].WriteLine(Line,addTime);
            return true;
         }
         else
         {
            return false;   
         }
      }
      
      [color=#0000ff]public[/color] void WriteLine( string Line, bool addTime )
      {
         Sessions[ActiveLog].WriteLine(Line,addTime);
      }
      
      [color=#0000ff]public[/color] bool WriteEmptyLine( int ID )
      {
         if (!( Sessions[ID] == null ))
         {
            Sessions[ID].WriteLine("",false);
            return true;
         }
         else
         {
            return false;   
         }         
      }
      
      [color=#0000ff]public[/color] void WriteEmptyLine()
      {
         Sessions[ActiveLog].WriteLine("",false);         
      }
      
      [color=#0000ff]public[/color] bool AddSpace( char Space, byte lenght, int ID )
      {
         if (!( Sessions[ID] == null ))
         {
            Sessions[ID].AddSpace(Space,lenght);
            return true;            
         }
         else
         {
            return false;
         }         
      }
      
      [color=#0000ff]public[/color] void AddSpace( char Space, byte lenght )
      {
         Sessions[ActiveLog].AddSpace(Space,lenght);         
      }
      
      [color=#0000ff]public[/color] int ChangeActiveLog( int ID )
      {
[color=#00ff00]         /*
          * Diese Funktion testet ob die LogSession vorhanden ist,
          * wenn die Log vorhanden ist wird die Aktive LogSession
          * gewechselt. Sollte dies nicht der Fall sein wird die
          * Aktuelle LogSession zurückgegeben.
          */[/color]
          
          if ( TestLog(ID) == false )
         {
            return ActiveLog;
         }
         else
         {
            return ActiveLog = ID;
         }
      }
      
      [color=#0000ff]public[/color] bool TestLog( int ID )
      {
[color=#00ff00]         /*
          * Diese Funktion testet ob die Log vorhanden ist.
          * Wenn sie vorhanden ist wird getestet ob die Logdatei
          * noch Aktiv ist.
          */[/color]
          
         if ( Sessions[ID] == null )
         {
            return false;
         }
         else
         {
            return Sessions[ID].Active;
         }         
      }
      
      [color=#0000ff]public[/color] bool DeactivateLog( int ID )
      {
[color=#00ff00]         /*
          * Diese Funktion Deaktiviert eine Logdatei wenn diese
          * erzeugt ist und nicht schon Deaktiviert wurde.
          */[/color]
         if ( ID == ActiveLog )
            ActiveLog = 0;
         if (( Sessions[ID] == null ) & !( ID == 0 ))
         {
            return false;
         }
         else
         {
            return Sessions[ID].CloseLog();
         }
      }
      
      [color=#0000ff]public[/color] StringCollection GetLog( int ID )
      {
[color=#00ff00]         /*
          * Diese Funktion gibt die Logdatei zurück, wenn diese
          * vorhanden ist. Sollte sie nicht vorhanden sein wird
          * null zurückgegeben.
          */[/color]
         if ( Sessions[ID] == null )
         {
            return null;
         }
         else
         {
            return Sessions[ID].GetLog();
         } 
      }
      
      [color=#0000ff]public[/color] StringCollection GetLog()
      {
[color=#00ff00]         /*
          * Diese Funktion gibt die Logdatei zurück, wenn diese
          * vorhanden ist. Sollte sie nicht vorhanden sein wird
          * null zurückgegeben.
          */[/color]
         if ( Sessions[ActiveLog] == null )
         {
            return null;
         }
         else
         {
            return Sessions[ActiveLog].GetLog();
         } 
      }      
      
      [color=#0000ff]public[/color] int GetLeakCount( int ID )
      {
         if ( Sessions[ID] == null )
         {
            return 0;
         }
         else
         {
            return Sessions[ID].LeakCount;
         } 
      }
      
      [color=#0000ff]public[/color] int GetLeakCount()
      {
         if ( Sessions[ActiveLog] == null )
         {
            return 0;
         }
         else
         {
            return Sessions[ActiveLog].LeakCount;
         } 
      }
      
[color=#00ff00]      /*
       * Die Folgenden Codezeilen sind für das Speichern von Logdateien
       * zuständig.
       */[/color]
      
      [color=#0000ff]private[/color] bool CreatePath( string pPath )
      {
         try
         {
            DirectoryInfo DI = new DirectoryInfo(pPath);
            if ( DI.Exists == true )
            {
               return true;
            }
            else
            {
               DI.Create();
               return true;
            }
         }
         catch (Exception)
         {
            return false;
         }
      }
      
      [color=#0000ff]private[/color] string ConnectPath( string pPath, string nFold )
      {
         if ( nFold != "" )
         {
            if ( pPath.EndsWith("\\") == true )
            {
               return (pPath + nFold + "\\");
            }
            else
            {
               return (pPath + "\\" + nFold + "\\");
            }
         }
         else return pPath;
      }
      
      [color=#0000ff]private[/color] bool SaveLogAs( StringCollection Lines, string fPath, string Name )
      {
         try
         {
              using (FileStream fs = new FileStream(string.Format("{0}{1}.log",
                                               fPath,Name),FileMode.OpenOrCreate))
               {
                  TextWriter writer = new StreamWriter(fs);
                  foreach (string line in Lines)
                  {
                     writer.WriteLine(line);
                  }
               writer.Flush();
               }
            return true;
         }
         catch (Exception)
         {
            return false;
         }
      }
      
      [color=#0000ff]private[/color] bool SaveAllLog( string pPath )
      {
         if ( this.CreatePath(pPath) == true )
         {
            bool Result;
            Result = true;
            foreach (Session SE in Sessions)
            {
               if (this.SaveLogAs(SE.GetLog(),pPath,SE.Name) == false)
                  Result = false;
            }
            return Result;
         }
         else return false;         
      }
      
      [color=#0000ff]private[/color] bool SingleSaveAll( string pPath, string Name )
      {
         if ( this.CreatePath(pPath) == true )
         {
            StringCollection tmpSC = new StringCollection();
            foreach (Session SE in Sessions)
            {
               foreach (string Line in SE.GetLog())
               {
                  tmpSC.Add(Line);
               }
               tmpSC.Add("");
               tmpSC.Add("");
            }
            return this.SaveLogAs(tmpSC,pPath,Name);   
         }
         else return false;
      }
      
      [color=#0000ff]public[/color] bool SaveSingleAs( int SingleID, string pPath, string Name )
      {
[color=#00ff00]         /*
          * Speichert eine Einzellne Logdatei unter angegebenen Pfad
          * und Namen.
          */[/color]
         
          if (( this.CreatePath(pPath) == true ) &
              !( this.Sessions[SingleID] == null ))
         {
            return this.SaveLogAs(this.Sessions[SingleID].GetLog(),pPath,Name);
         }
         else
         {
             return false;
         }
      }
      
      [color=#0000ff]public[/color] bool SaveSingleAs( int SingleID, string Name )
      {
[color=#00ff00]         /*
          * Speichert eine Eizellne Logdatei unter angegebenen Namen.
          * Der Pfad ist der Aktuell vom Programm benutzte.
          */[/color]
         if (!( this.Sessions[SingleID] == null ))
         {
            return this.SaveLogAs(this.Sessions[SingleID].GetLog(),
                               System.Environment.CurrentDirectory+"\\",Name);
         }
         else return false;
      }
      
      [color=#0000ff]public[/color] bool SaveSingle( int SingleID, string pPath )
      {
[color=#00ff00]         /*
          * Speichert eine Einzellne Logdatei unter dem beim erzeugen
          * angegebenen Namen und dem als Paramater mitgegebenen Pfad.
          */[/color]
         
         // Code
         
         if (( this.CreatePath(pPath) == true ) &
              !( this.Sessions[SingleID] == null ))
         {
            return this.SaveLogAs(this.Sessions[SingleID].GetLog(),
                                  pPath,this.Sessions[SingleID].Name);
         }
         else return false;
      }
      
      [color=#0000ff]public[/color] bool SaveSingle( int SingleID )
      {
[color=#00ff00]         /*
          * Speichert eine Einzellne Logdatei unter dem beim erzeugen
          * angegebenen Namen und dem Aktuell vom Programm benutzten
          * Pfad.
          */[/color]
          
         if (!( this.Sessions[SingleID] == null ))
         {         
            return this.SaveLogAs(this.Sessions[SingleID].GetLog(),
                              System.Environment.CurrentDirectory+"\\",
                              this.Sessions[SingleID].Name);
         }
         else return false;   
      }
      
      [color=#0000ff]public[/color] bool SaveAs( string pPath, string Name )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einem Ordner mit dem als
          * Parameter angegebenen Namen und dem als Parameter
          * angegebenen Pfad
          */[/color]
         string tmpPath;
         tmpPath = this.ConnectPath(pPath,Name);
         
         return this.SaveAllLog(tmpPath);
      }
      
      [color=#0000ff]public[/color] bool SaveAs( string Name )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einem Ordner mit dem als
          * Parameter angegebenen Namen und dem aktuell vom
          * Programm verwendeten Pfad.
          */[/color]
         string tmpPath;
         tmpPath = this.ConnectPath(Environment.CurrentDirectory+"\\",Name);
         
         return this.SaveAllLog(tmpPath);             
      }
      
      [color=#0000ff]public[/color] bool Save( string pPath )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einem Ordner mit dem beim
          * erzeugen der ersten Logdatei angegebenen Namen und dem
          * als Parameter angegebenen Pfad.
          */
[/color]
         string tmpPath;
         tmpPath = this.ConnectPath(pPath,Sessions[0].Name);
         
         return this.SaveAllLog(tmpPath);             
      }
      
      [color=#0000ff]public[/color] bool Save()
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einem Ordner mit dem beim
          * erzeugen der ersten Logdatei angegebenen Namen und dem
          * aktuellen vom Programm verwendeten Pfad.
          */[/color]
         
         string tmpPath;
         tmpPath = this.ConnectPath(Environment.CurrentDirectory+"\\",
                                                 Sessions[0].Name);
         
         return this.SaveAllLog(tmpPath);          
      }
      
      [color=#0000ff]public[/color] bool SaveAllAs( string pPath, string Name )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einer Datei mit dem als
          * Parameter übergebenen Namen und dem als Parameter
          * übergebenen Pfad.
          */[/color]
         
          return this.SingleSaveAll(pPath,Name);
      }

      [color=#0000ff]public[/color] bool SaveAllAs( string Name )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einer Datei mit dem als
          * Parameter übergebenen Namen und dem aktuell vom
          * Programm verwendeten Pfad.
          */[/color]
         

         return this.SingleSaveAll(System.Environment.CurrentDirectory+"\\",Name);
      }
      
      [color=#0000ff]public[/color] bool SaveAll( string pPath )
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einer Datei mit dem
          * beim erzeugen der ersten Logdatei angegebenen Namen
          * und dem als Parameter übergebenen Pfad
          */[/color]
         
          return this.SingleSaveAll(pPath,Sessions[0].Name);
      }
      
      [color=#0000ff]public[/color] bool SaveAll()
      {
[color=#00ff00]         /*
          * Speichert alle Logdateien in einer Datei mit dem
          * beim erzeugen der ersten Logdatei angegebenen Namen
          * und dem aktuell vom Programm verwendeten Pfad.
          */[/color]
         
          return this.SingleSaveAll(System.Environment.CurrentDirectory+"\\",
                                                      Sessions[0].Name);
      }
   }
}
Angehängte Dateien
Dateityp: pdf logger_206.pdf (78,4 KB, 25x aufgerufen)
Dateityp: zip ulog_105.zip (3,0 KB, 25x aufgerufen)
Felix K.
Zitat:
Siehst du diesen Park da unten?
Jeden Tag lernen sich leute kennen und verlassen einander, und du hast dein ganzes Leben Zeit darin zu gehen!
  Mit Zitat antworten Zitat
Antwort Antwort

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:49 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz