Bonjour,

Voila je dois créer une classe EntJournal avec une méthode Trace qui prend mon exception EntException (que j'ai créer avant )

Je dois réaliser un constructeur qui definira le type de journalisation qui seront dans l'app.config avec les clés suivantes:
<add key = "EntJournal" value= "JournalType= windows;Name=MonAppli"/>
et

<add key = "EntJournal" value= "JournalType=Log;Name=MonAppli";Folder=c:/test;SaveUpTo= 30/>
Dans ces journaux je dois mettre les exceptions de mon appli. pour le fichier log il est sous la forme yyyyMMdd_HHmmss_monappli.log et je dois en conserver que 30 .


Voila le code que j'ai écrit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
Public Class EntJournal
 
 
 
    Sub New()
        Dim reponse As String = ""
        reponse = ConfigurationManager.AppSettings.Get("EntJournal")
 
        If reponse.Contains("JournalType=Windows") Then
            Dim NomExe As String = ""
            NomExe = Application.ExecutablePath
            Dim nomAppli
            nomAppli = reponse.Replace("JournalType=Windows;Name=", "")
            EventLog.CreateEventSource(NomExe, nomAppli)
 
        ElseIf reponse.Contains("JournalType=Log") Then
            Dim nomApplitempo As String = reponse.Replace("JournalType=Log;Name=", "")
            nomApplitempo.Split(";")
            Dim nomAppli As String = nomApplitempo(0)
            Dim folderTargetTempo As String = nomApplitempo(1)
            Dim folder As String = folderTargetTempo.Replace("Folder=", "")
            Dim saveupTemporaire As String = ""
            saveupTemporaire = nomApplitempo(2)
            Dim nbLog As Integer = CType(saveupTemporaire.Replace("SaveUpTo=", ""), Integer)
            If System.IO.Directory.Exists(folder) = False Then
                System.IO.Directory.CreateDirectory(folder)
            End If
            Dim dateLog As String = TextWriter.Instance.nomDufichier(nomAppli)
            Dim objStream As FileStream = New FileStream(folder & dateLog & nomAppli & ".log", FileMode.OpenOrCreate)
            objStream.Close()
            listeFichierLog(folder, nbLog)
 
 
            End If
    End Sub
    Public Sub TraceLine(ByVal ExceptionEnt As EntException)
 
        ' /////////////// Pour écrire dans le fichier log  ////////////////'
        Dim reponse As String = ""
        reponse = ConfigurationManager.AppSettings.Get("EntJournal")
        Dim nomApplitempo As String = reponse.Replace("JournalType=Log;Name=", "")
        nomApplitempo.Split(";")
        Dim nomAppli As String = nomApplitempo(0)
        Dim folderTargetTempo As String = nomApplitempo(1)
        Dim folder As String = folderTargetTempo.Replace("Folder=", "")
        Dim saveupTemporaire As String = ""
        saveupTemporaire = nomApplitempo(2)
        Dim nbLog As Integer = CType(saveupTemporaire.Replace("SaveUpTo=", ""), Integer)
        If System.IO.Directory.Exists(folder) = False Then
            System.IO.Directory.CreateDirectory(folder)
        End If
        Dim DateLog As String = TextWriter.Instance.nomDufichier(nomAppli)
        Dim objStream As FileStream = New FileStream(folder & DateLog & nomAppli & ".log", FileMode.OpenOrCreate)
        Dim objTraceListener As TextWriterTraceListener = New TextWriterTraceListener(objStream)
        Trace.Listeners.Add(objTraceListener)
 
        Dim ligneAEcrire As String = ""
        ligneAEcrire = DateLog & "Nouvelle exception de type" & (ExceptionEnt.TYPE.ToString) & ".Code numéro : " & ExceptionEnt.CODE & " . " & ExceptionEnt.InnerException.Message & ":" & ExceptionEnt.Message
        Trace.WriteLine(ligneAEcrire)
        Trace.AutoFlush = True
        objStream.Close()
 
        ' /////////////// Pour écrire dans le journal windows ////////////////'
        Dim objListener As EventLogTraceListener = New EventLogTraceListener
        Dim objLog As EventLog = New EventLog(Date.Now & "Nouvelle exception de type" & (ExceptionEnt.TYPE.ToString) & ".Code numéro : " & ExceptionEnt.CODE & " . " & ExceptionEnt.InnerException.Message & ":" & ExceptionEnt.Message)
        objListener.EventLog = objLog
 
    End Sub
 
    Private Sub listeFichierLog(ByVal folder As String, ByVal nbfichier As Integer)
        Dim sortie() As String
        sortie = System.IO.Directory.GetFiles(folder)
        If sortie.Count >= nbfichier Then
            Dim fichierASupprimer As String = sortie(30)
            File.Delete(fichierASupprimer)
        End If
    End Sub
 
End Class
 
 
Imports System.Globalization
 
Public Class TextWriter
    Inherits TextWriterTraceListener
 
    Private Shared s_Instance As TextWriter
    Private Shared s_InstanceLocker As Object = New Object
 
    Public Shared ReadOnly Property Instance() As TextWriter
        Get
            SyncLock s_InstanceLocker
                If (s_Instance Is Nothing) Then
                    s_Instance = New TextWriter
                End If
            End SyncLock
            Return s_Instance
        End Get
    End Property
 
 
    Public Function nomDufichier(ByVal nomAppli As String) As String
        Dim dateLog As String = ""
 
        dateLog = Date.Now.ToString("yyyyMMdd_HHmmss_")
 
        Return dateLog
    End Function
End Class
Qu'en pensez vous ?

cordialement,
Kévin