Created
February 28, 2013 07:28
-
-
Save mabuenox/5054939 to your computer and use it in GitHub Desktop.
Write Log on Windows Service. Only a guide, this code can be enhanced.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void EscribirLog(string message) | |
{ | |
try | |
{ | |
DirectoryInfo dirInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Log"); | |
if (!dirInfo.Exists) | |
{ | |
dirInfo.Create(); | |
} | |
FileInfo[] files = dirInfo.GetFiles(); | |
foreach (FileInfo file in files) | |
{ | |
if (file.LastWriteTime < DateTime.Today.AddDays(-7)) | |
{ | |
file.IsReadOnly = false; | |
file.Delete(); | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
} | |
finally | |
{ | |
string nombreFichero = DateTime.Today.ToShortDateString(); | |
nombreFichero = nombreFichero.Replace("/", "-"); | |
// Append new text to an existing file | |
using (System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Log" + "\\" + nombreFichero + ".txt", true)) | |
{ | |
file.WriteLine(DateTime.Now.ToString() + " " + message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment