Bonjour à tous,

j'ai une petit soucis concernant ma classe d'écriture de log, j'ai parfois une exception du type :
L'opération demandée n'a pu s'accomplir sur un fichier ayant une section mappée utilisateur ouverte.
De plus je trouve mon code "sale", si quelqu'un pouvais me dire comment il ferais mieux.

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
public enum LogType
    {
        Fault,
        Information,
        Error
    };
 
    public class LogFile
    {
        #region Fields
        private String Path = String.Empty;
        #endregion
        #region Constructor
        public LogFile(String Path)
        {
            this.Path = Path;
        }
        #endregion
 
        public void Write(String UserName, String Method, String Text, LogType Type)
        {
            Text = Text.Replace("é", "é");
            Text = Text.Replace("è", "è");
            Text = Text.Replace("à", "à");
            Text = Text.Replace("ê", "ê");
            Text = Text.Replace("ô", "ô");
            Text = Text.Replace("'", "'");
 
            lock (this)
            {
                DateTime Now = DateTime.Now;
                String FileName = Now.Year.ToString() + "-" + Now.Month.ToString().PadLeft(2, '0') + "-" + Now.Day.ToString().PadLeft(2, '0') + ".log.html";
                String Path = System.IO.Path.Combine(this.Path, FileName);
                String Content = String.Empty;
                if (!File.Exists(Path))
                    Content += "<table>\r\n";
                else
                {
 
                    Content = File.ReadAllText(Path);
                }
                if (Content.EndsWith("</table>\r\n"))
                    Content = Content.Replace("</table>\r\n", "");
                String Color = "White";
                if (Type == LogType.Error)
                    Color = "Red";
                else if (Type == LogType.Fault)
                    Color = "Yellow";
                Content += "\t<tr bgcolor=\"" + Color + "\">\r\n";
                Content += "\t\t<td width=\"10%\">" + "[" + Now.Hour.ToString().PadLeft(2, '0') + ":" + Now.Minute.ToString().PadLeft(2, '0') + ":" + Now.Second.ToString().PadLeft(2, '0') + "]</td>\r\n";
                Content += "\t\t<td width=\"15%\">" + Type.ToString() + "</td>\r\n";
                Content += "\t\t<td width=\"15%\">" + UserName + "</td>\r\n";
                Content += "\t\t<td width=\"30%\">" + Method + "</td>\r\n";
                Content += "\t\t<td width=\"30%\">" + Text + "</td>\r\n";
                Content += "\t</tr>\r\n";
                Content += "</table>\r\n";
                File.WriteAllText(Path, Content);
            }
        }
Et pour l'utilisation, je fais une classe Singleton dérivée de celle-ci.

Bon déjà, je pourrais ne pas ouvrir / fermer le fichier à chaque fois, et utiliser un StreamReader /Writter, vu que les opérations d'ouverture / fermeture sont couteuses. Et puis utiliser un StringBuilder.

D'autre suggestions ?

Merci d'avance.

Cordialement,
NeoKript