public static void LogProcessor___Valid___Works() { var tempFileName = Path.GetTempFileName(); try { // Arrange var configuration = new FileLogConfig(LogInclusionKindToOriginsMaps.ExceptionsFromAnywhere, tempFileName); var processor = new FileLogWriter(configuration); var infoCanary = A.Dummy <string>(); var errorCanary = new ArgumentException(A.Dummy <string>()); // Act processor.Log(infoCanary.ToLogEntry().ToLogItem(LogItemOrigin.ItsLogEntryPosted)); processor.Log(errorCanary.ToLogEntry().ToLogItem(LogItemOrigin.ItsLogEntryPosted)); // Assert var fileContents = File.ReadAllText(tempFileName); fileContents.Should().NotContain(infoCanary); fileContents.Should().Contain(errorCanary.Message); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } }
private void ReadFileLogConfig() { System.Xml.XmlNode file = this.xmlNode.SelectSingleNode("//File"); if (file != null) { System.Xml.XmlNodeList configs = file.SelectNodes("Config"); foreach (System.Xml.XmlNode config in configs) { //id="stack" category="" level="DEBUG" module="stack" fileName="stack.log" System.Xml.XmlAttribute id = config.Attributes["id"]; System.Xml.XmlAttribute category = config.Attributes["category"]; System.Xml.XmlAttribute level = config.Attributes["level"]; System.Xml.XmlAttribute module = config.Attributes["module"]; System.Xml.XmlAttribute fileName = config.Attributes["fileName"]; if (id == null || fileName == null) { throw new StackException("config error: file config should have id and fileName."); } // create a config instance FileLogConfig fileLogConfig = new FileLogConfig(); fileLogConfig.Category = category.Value; fileLogConfig.Level = (LogLevel)Enum.Parse(typeof(LogLevel), level.Value); fileLogConfig.Module = module.Value; fileLogConfig.FileName = fileName.Value; if (this.logConfigs.ContainsKey(id.Value)) { throw new StackException("the id of config must be unique."); } this.logConfigs.Add(id.Value, fileLogConfig); } } }
/// <summary> /// Initializes a new instance of the <see cref="FileLogWriter"/> class. /// </summary> /// <param name="fileLogConfig">Configuration.</param> /// <param name="logItemSerializerFactory">Optional serializer factory; DEFAULT will be base version.</param> public FileLogWriter( FileLogConfig fileLogConfig, ISerializerFactory logItemSerializerFactory = null) : base(fileLogConfig, logItemSerializerFactory) { this.fileLogConfig = fileLogConfig ?? throw new ArgumentNullException(nameof(fileLogConfig)); var directoryPath = Path.GetDirectoryName(this.fileLogConfig.LogFilePath); if (string.IsNullOrWhiteSpace(directoryPath)) { throw new ArgumentException(Invariant($"directory name from {nameof(this.fileLogConfig)}.{nameof(FileLogConfig.LogFilePath)} is null or white space")); } bool didCreateDirectory; if (this.fileLogConfig.CreateDirectoryStructureIfMissing && !Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); didCreateDirectory = true; } else { didCreateDirectory = false; } // this is to capture the directory creation info as well as prevent inconsistent syncronization usage of this.fileLogConfig... this.thisToString = Invariant($"{this.GetType().ToStringReadable()}; {nameof(this.fileLogConfig.LogInclusionKindToOriginsMap)}: {this.fileLogConfig.LogInclusionKindToOriginsMapFriendlyString}; {nameof(this.fileLogConfig.LogFilePath)}: {this.fileLogConfig.LogFilePath}; {nameof(this.fileLogConfig.CreateDirectoryStructureIfMissing)}: {this.fileLogConfig.CreateDirectoryStructureIfMissing}; {nameof(didCreateDirectory)}: {didCreateDirectory}"); }
private static IResultLogger BuildFileLogger() { var config = new FileLogConfig("FileLogger") { LogLocation = @"..\..\..", Level = LoggingLevels.Quiet }; return(HLogging.FileLogger(config)); }
public static void LogConfigurationFileConstructor___Valid___Works() { // Arrange var contextsToLog = LogInclusionKindToOriginsMaps.StringAndObjectsFromItsLogEntryPosted; var filePath = Path.GetTempFileName(); // Act var actual = new FileLogConfig(contextsToLog, filePath); // Assert actual.Should().NotBeNull(); actual.LogFilePath.Should().Be(filePath); }