For those who want to log event messages in a file and also see them in the debug output window, here is a snippet of code that I created.
Please use this only in the development environment, this code is not optimized.
using:
In asp.net, add a link to this library (MyPresentationLayer.Web).
Add the following element to the 'rewriter' node:
<register logger="IntelligenciaExt.Web.Logging.UrlRewriterIntelligencia.FileLogger, IntelligenciaExt.Web"/>
By default, the log file can be found outside of your www folder, in a subfolder of intelligentenciaLog.
using System; using SysDiag = System.Diagnostics; using System.IO; using Intelligencia.UrlRewriter.Logging; namespace MyPresentationLayer.Web.Logging.UrlRewriterIntelligencia { /// <summary> /// Custom logger for Intelligencia UrlRewriter.net that logs messages /// to a plain text file (../intelligenciaLog/log.txt). /// </summary> public class FileLogger : IRewriteLogger { private const string _logFolderName = "../intelligenciaLog"; private const string _logFileName = "log.txt"; private const string _appName = "UrlRewriterIntelligencia.FileLogger"; public FileLogger() { LogToFile(Level.Info, "Created new instance of class 'FileLogger'"); } public void Debug(object message) { LogToFile(Level.Debug, (string)message); } public void Error(object message, Exception exception) { string errorMessage = String.Format("{0} ({1})", message, exception); LogToFile(Level.Error, errorMessage); } public void Error(object message) { LogToFile(Level.Error, (string)message); } public void Fatal(object message, Exception exception) { string fatalMessage = String.Format("{0} ({1})", message, exception); LogToFile(Level.Fatal, fatalMessage); } public void Info(object message) { LogToFile(Level.Info, (string)message); } public void Warn(object message) { LogToFile(Level.Warn, (string)message); } private static void LogToFile(Level level, string message) { string outputMessage = String.Format("[{0} {1} {2}] {3}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), _appName.PadRight(50, ' '), level.ToString().PadRight(5, ' '), message); SysDiag.Debug.WriteLine(outputMessage); try { string pathToLogFolder =Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _logFolderName); if (!Directory.Exists(pathToLogFolder)) { Directory.CreateDirectory(pathToLogFolder); } string fullPathToLogFile = Path.Combine(pathToLogFolder, _logFileName); using (StreamWriter w = File.AppendText(fullPathToLogFile)) { w.WriteLine(outputMessage); // Update the underlying file. w.Flush(); // Close the writer and underlying file. w.Close(); } } catch (Exception) { } } internal enum Level { Warn, Fatal, Info, Error, Debug } } }
source share