using NLog; using NLog.Config; using NLog.Targets; using System; using System.Data.Entity.Validation; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; namespace Atesa.Utilitarios { public class Log { private LogEventInfo vLogEventInfo; [MethodImpl(MethodImplOptions.NoInlining)] public Log(string vLoggerName) { this.LoggerName = vLoggerName; string name = Assembly.GetCallingAssembly().GetName().Name; //string valueKeyRegedit = new ObtieneParametros().GetValueKeyRegedit("RutaLogGenerados", false); string valueKeyRegedit = "C:\\ATESA\\"; //string str3 = new ObtieneParametros().GetValueKeyRegedit("LogLevel", false); string str3 = "warn"; LoggingConfiguration configuration = LogManager.Configuration; configuration = (configuration == null) ? new LoggingConfiguration() : configuration; FileTarget Target = new FileTarget { Name = vLoggerName, Layout = "${date}|${machinename}|${level}|${logger}|${event-context:item=INITLogger}|${message}", FileName = valueKeyRegedit + "/" + name + "/${logger} ${shortdate}.txt" }; if (!((configuration.ConfiguredNamedTargets == null) || configuration.ConfiguredNamedTargets.Any(c => (c.Name == Target.Name)))) { configuration.AddTarget(Target); } NLog.LogLevel trace = NLog.LogLevel.Trace; switch (str3.Trim().ToLower()) { case "warn": trace = NLog.LogLevel.Warn; break; case "error": trace = NLog.LogLevel.Error; break; default: trace = NLog.LogLevel.Trace; break; } LoggingRule rule = new LoggingRule(vLoggerName, trace, Target); if (!configuration.LoggingRules.Any(c => (c.LoggerNamePattern == rule.LoggerNamePattern))) { configuration.LoggingRules.Add(rule); } LogManager.Configuration = configuration; } //[MethodImpl(MethodImplOptions.NoInlining)] //public void Error(DbEntityValidationException ex) //{ // StackTrace trace = new StackTrace(new StackFrame(1)); // this.MethodName = trace.GetFrame(0).GetMethod().Name; // StringBuilder builder = new StringBuilder(); // foreach (string str in (from x in ex.EntityValidationErrors // select x.ValidationErrors into x // select x.ErrorMessage).ToList()) // { // builder.AppendLine(str); // } // builder.AppendLine(" StackTrace: "); // builder.Append(ex.StackTrace); // this.vLogEventInfo = new LogEventInfo(NLog.LogLevel.Error, this.LoggerName, " DbEntityValidationException Message: " + builder.ToString()); // this.vLogEventInfo.Properties["INITLogger"] = this.INITLogger; // this.logger.Log(this.vLogEventInfo); // builder.Clear(); //} [MethodImpl(MethodImplOptions.NoInlining)] public void Error(Exception ex) { StackTrace trace = new StackTrace(new StackFrame(1)); this.MethodName = trace.GetFrame(0).GetMethod().Name; StringBuilder exceptionMessages = this.GetExceptionMessages(ex); this.vLogEventInfo = new LogEventInfo(NLog.LogLevel.Error, this.LoggerName, exceptionMessages.ToString()); this.vLogEventInfo.Properties["INITLogger"] = this.INITLogger; this.logger.Log(this.vLogEventInfo); exceptionMessages.Clear(); } [MethodImpl(MethodImplOptions.NoInlining)] public void Error(string Message) { StackTrace trace = new StackTrace(new StackFrame(1)); this.MethodName = trace.GetFrame(0).GetMethod().Name; Message = Message.Replace(Environment.NewLine, " -> "); this.vLogEventInfo = new LogEventInfo(NLog.LogLevel.Error, this.LoggerName, Message); this.vLogEventInfo.Properties["INITLogger"] = this.INITLogger; this.logger.Log(this.vLogEventInfo); } public StringBuilder GetExceptionMessages(Exception ex) { StringBuilder builder = new StringBuilder(); while (ex != null) { builder.AppendLine(" Message: "); builder.Append(ex.Message); builder.AppendLine(" StackTrace: "); builder.Append(ex.StackTrace); ex = ex.InnerException; } return builder; } [MethodImpl(MethodImplOptions.NoInlining)] public void Trace(string Message) { StackTrace trace = new StackTrace(new StackFrame(1)); this.MethodName = trace.GetFrame(0).GetMethod().Name; Message = Message.Replace(Environment.NewLine, " -> "); this.vLogEventInfo = new LogEventInfo(NLog.LogLevel.Trace, this.LoggerName, Message); this.vLogEventInfo.Properties["INITLogger"] = this.INITLogger; this.logger.Log(this.vLogEventInfo); } private Logger logger { get { return LogManager.GetLogger(this.LoggerName); } } public string IDLogger { get; set; } public string LoggerName { get; set; } private string MethodName { get; set; } //private string INITLogger = "${this.MethodName}|{this.IDLogger}"; private string INITLogger { get { return string.Format("{0}|{1}", this.MethodName, this.IDLogger); } } } }