protected override void WriteLog(LogEntry entry, Stream stream) { using(var writer = new StreamWriter(stream, System.Text.Encoding.UTF8)) { writer.WriteLine(entry.ToString()); } }
public void Log(LogEntry entry) { if(entry == null) return; //根据日志级别来调整控制台的前景色 switch(entry.Level) { case LogLevel.Trace: Console.ForegroundColor = ConsoleColor.Gray; break; case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.DarkGray; break; case LogLevel.Warn: Console.ForegroundColor = ConsoleColor.Yellow; break; case LogLevel.Error: case LogLevel.Fatal: Console.ForegroundColor = ConsoleColor.Red; break; } try { //打印日志信息 Console.WriteLine(entry); } finally { //恢复默认颜色 Console.ResetColor(); } }
protected override void WriteLog(LogEntry entry, Stream stream) { //注意:此处不能关闭stream参数传入的流,该流由基类确保安全释放! using(var writer = new StreamWriter(stream, System.Text.Encoding.UTF8, 1024 * 16, true)) { writer.WriteLine(entry.ToString()); } }
public void Log(LogEntry entry) { if(entry == null) return; var filePath = this.ResolveSequence(entry); if(string.IsNullOrWhiteSpace(filePath)) throw new InvalidOperationException("Unspecified path of the log file."); //将日志实体加入内存队列中 _queue.Enqueue(entry); //从线程池拉出一个后台线程进行具体的日志记录操作 ThreadPool.QueueUserWorkItem(this.LogFile, filePath); }
private string ResolveSequence(LogEntry entry) { const string PATTERN = @"(?<no>\d+)"; const string SEQUENCE = "{sequence}"; var maximum = 0; var result = string.Empty; var filePath = this.GetFilePath(entry); if(string.IsNullOrEmpty(filePath) || (!filePath.Contains(SEQUENCE))) return filePath; if(this.FileSize < 1) return filePath.Replace(SEQUENCE, string.Empty); var fileName = System.IO.Path.GetFileName(filePath); var infos = Zongsoft.IO.LocalFileSystem.Instance.Directory.GetFiles(System.IO.Path.GetDirectoryName(filePath), fileName.Replace(SEQUENCE, "|" + PATTERN + "|"), false); var pattern = string.Empty; int index = 0, position = 0; while((index = fileName.IndexOf(SEQUENCE, index)) >= 0) { if(index > 0) pattern += Zongsoft.IO.LocalFileSystem.LocalDirectoryProvider.EscapePattern(fileName.Substring(position, index - position)); pattern += PATTERN; index += SEQUENCE.Length; position = index; } if(position < fileName.Length) pattern += Zongsoft.IO.LocalFileSystem.LocalDirectoryProvider.EscapePattern(fileName.Substring(position)); //设置正则匹配模式为完整匹配 if(pattern != null && pattern.Length > 0) pattern = "^" + pattern + "$"; foreach(var info in infos) { var match = System.Text.RegularExpressions.Regex.Match(info.Name, pattern); if(match.Success) { var number = int.Parse(match.Groups["no"].Value); if(number > maximum) { maximum = number; if(info.Size < this.FileSize * KB) result = info.Path.Url; } } } if(string.IsNullOrEmpty(result)) return filePath.Replace(SEQUENCE, (maximum + 1).ToString()); return result; }
protected virtual string GetFilePath(LogEntry entry) { var filePath = string.Empty; if(string.IsNullOrWhiteSpace(_filePath)) filePath = string.IsNullOrWhiteSpace(entry.Source) ? string.Empty : entry.Source + ".log"; else filePath = Logger.TemplateManager.Evaluate<string>(_filePath.Trim(), entry); if(string.IsNullOrWhiteSpace(filePath)) return null; filePath = filePath.Replace((Path.DirectorySeparatorChar == '/' ? '\\' : '/'), Path.DirectorySeparatorChar).Trim(); if(filePath[0] == '/' || filePath[0] == '\\') { filePath = Path.Combine(Path.GetPathRoot(Zongsoft.ComponentModel.ApplicationContextBase.Current.ApplicationDirectory), filePath.Substring(1)); if(!Directory.Exists(Path.GetDirectoryName(filePath))) Directory.CreateDirectory(Path.GetDirectoryName(filePath)); return filePath; } string directoryPath; if(filePath.StartsWith("~/") || filePath.StartsWith("~\\")) directoryPath = Zongsoft.ComponentModel.ApplicationContextBase.Current.EnsureDirectory(Path.GetDirectoryName(filePath.Substring(2))); else directoryPath = Zongsoft.ComponentModel.ApplicationContextBase.Current.EnsureDirectory(Path.GetDirectoryName(filePath)); return Path.Combine(directoryPath, Path.GetFileName(filePath)); }
protected abstract void WriteLog(LogEntry entry, Stream output);
private static void Log(LogEntry entry) { if(entry == null || _handlers == null) return; System.Threading.Tasks.Parallel.ForEach(_handlers, handler => { if(handler != null) handler.Handle(entry); }); }
protected virtual void Log(LogEntry entry) { if(entry == null) return; var loggers = _selector.Select(_parameter); System.Threading.Tasks.Parallel.ForEach(loggers, logger => { if(logger != null) logger.Log(entry); }); }
public void Log(LogEntry entry) { var dataAccess = this.DataAccess; if(dataAccess == null) return; var storage = this.Storage; Zongsoft.Collections.IQueue queue = null; foreach(Zongsoft.Options.Configuration.SettingElement parameter in _configuration.Parameters) { Logger.TemplateManager.Evaluate<string>(parameter.Value, entry); } throw new NotImplementedException(); }
public void Handle(LogEntry entry) { this.Execute(entry); }