-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
85 lines (78 loc) · 2.74 KB
/
Logger.cs
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
using System;
using System.Diagnostics;
using System.IO;
namespace Repos.Log
{
public static class Logger
{
// Cambie estos valores para su aplicación
public static readonly string ReposFolder = @"c:\Repos\logs\";
public static string LogPrefix
{
get;
set;
} = "repos";
private static Stopwatch timer;
private static bool isStartTrivial;
private static bool isStartDataUpdate;
private static bool isStartConnection;
private static void Writer(string file, string mensaje, ref bool firstRun, bool destroy)
{
string head = string.Empty;
if (!firstRun)
{
var dir = new DirectoryInfo(ReposFolder);
if (!dir.Exists)
{
dir.Create();
}
if (destroy)
{
if (File.Exists(ReposFolder + LogPrefix + @"Trivial.log.txt"))
{
File.Delete(ReposFolder + LogPrefix + @"Trivial.log.txt");
}
}
firstRun = true;
head = "=-=-=-=- [ " + DateTime.Now.ToString(@"yy-MM-dd") + " ] =-=-=-=-" + Environment.NewLine;
}
Debug.WriteLine(mensaje);
try
{
using (StreamWriter w = File.AppendText(ReposFolder + LogPrefix + file + @".log.txt"))
{
w.WriteLine(head + mensaje);
}
}
catch
{
// No se pudo escribir en el log
}
}
public static void Write(string mensaje)
{
bool x = true;
var mth = new StackTrace().GetFrame(1).GetMethod().ReflectedType.Name;
Writer("", DateTime.Now.ToString(@"yy-MM-ddtHH\:mm") + " (" + mth + ")\t\t" + mensaje, ref x, false);
}
public static void WriteDataUpdate(string mensaje)
{
Writer("DataUpdate", DateTime.Now.ToString(@"HH\:mm") + ":" + mensaje, ref isStartDataUpdate, false);
}
public static void WriteConnection(string mensaje)
{
Writer("Sync", DateTime.Now.ToString(@"HH\:mm") + ":" + mensaje, ref isStartConnection, false);
}
public static void Trivial(string mensaje)
{
var frame = new StackTrace().GetFrame(1).GetMethod();
if (!isStartTrivial)
{
timer = Stopwatch.StartNew();
Debug.Indent();
}
var s = timer.Elapsed.ToString() + " (" + frame + ")\t\t" + mensaje;
Writer("Trivial", s, ref isStartTrivial, true);
}
}
}