public void StreamLogger_Dispose() { #region Arrange var writer = new MemoryStream(); var logger = new StreamLogger(writer); var beforeCanWrite = logger.Writer.CanWrite; #endregion Arrange #region Act using (logger) { logger.Log(LogRecord.Empty); } #endregion Act #region Assert Assert.IsFalse(logger.Writer.CanWrite); Assert.AreNotEqual(beforeCanWrite, writer.CanWrite); #endregion Assert }
public void StreamLogger_DestructorDispose() { #region Arrange var writer = new MemoryStream(); var logger = new StreamLogger(writer); var beforeCanWrite = logger.Writer.CanWrite; #endregion Arrange #region Act logger.Log(LogRecord.Empty); logger = null; GC.Collect(); //Need wait for destructor call Task.Delay(50).Wait(); #endregion Act #region Assert Assert.AreNotEqual(beforeCanWrite, writer.CanWrite); #endregion Assert }
public void StreamLogger_Log() { #region Arrange var msg = new LogRecord(); var writer = new MemoryStream(); var serializer = new StringLogSerializer(); var logger = new StreamLogger(writer) { Serializer = serializer }; var expectedMsg = serializer.Serialize(msg) + Environment.NewLine; var beforeCanWrite = logger.Writer.CanWrite; #endregion Arrange #region Act logger.Log(msg); #endregion Act #region Assert Assert.AreEqual(expectedMsg, logger.Encoding.GetString(writer.ToArray())); Assert.AreEqual(beforeCanWrite, logger.Writer.CanWrite); #endregion Assert }
public void Error_Log_Call_Shoould_Write_File_Expected_Message__And_Path() { string message = LoggerProvider.CreateLogMessage(MessageLength.Normal); string expectedMessage = $"{_dateTimeProviderMock.UtcNow} [ERROR] {message}"; using (var stream = _streamFactoryMock.Create()) { var streamLogger = new StreamLogger(stream, _dateTimeProviderMock, _streamWriterMock); streamLogger.Log(LogLevel.Error, message); _streamWriterMock.Received().Writeline(Arg.Any <Stream>(), Arg.Is(expectedMessage)); } }
public void TestLog__MessageNull() { using (MemoryStream stream = new MemoryStream(new byte[0], true)) { StreamLogger streamLogger = new StreamLogger(stream); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { streamLogger.Log(null); }); Assert.Equal("message", ex.ParamName); } }
public void StreamLoggerTest() { const string message = "Test"; var memoryStream = new MemoryStream(); var reader = new StreamReader(memoryStream); var writer = new StreamWriter(memoryStream); var logger = new StreamLogger(writer); logger.Log(message); logger.Close(); memoryStream.Seek(0, SeekOrigin.Begin); Assert.Equal(message, reader.ReadLine()); Assert.Null(reader.ReadLine()); }
public void TestLog__AlreadyDisposed() { using (MemoryStream stream = new MemoryStream(new byte[0], true)) { StreamLogger streamLogger = new StreamLogger(stream); streamLogger.Dispose(); InvalidOperationException ex = Assert.Throws <InvalidOperationException>(delegate { streamLogger.Log(Substitute.For <ILogMessage>()); }); Assert.Contains("Object has already been disposed", ex.Message); } }
public void StreamLoggerTest_TestWriteSuccessful() { string result; using (var ms = new MemoryStream()) { using (StreamLogger logger = new StreamLogger(ms)) { logger.Log(LogLevel.Info, "Test message"); ms.Flush(); ms.Position = 0; using (var sr = new StreamReader(ms)) { result = sr.ReadToEnd(); } } } Assert.Contains("Test message", result); }
public void TestLog__Assert() { byte[] bytes = new byte[50]; using (MemoryStream stream = new MemoryStream(bytes, true)) { using (StreamLogger streamLogger = new StreamLogger(stream)) { streamLogger.Log(LogType.Assert, "a message"); } } using (MemoryStream stream = new MemoryStream(bytes, false)) { using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); Assert.Contains("[AST ", result); Assert.Contains("] a message", result); } } }
public void TestLog() { ILogMessage message = Substitute.For <ILogMessage>(); message.ToLogString().Returns("[OMG wtf] bbq"); byte[] bytes = new byte[15]; using (MemoryStream stream = new MemoryStream(bytes, true)) { using (StreamLogger streamLogger = new StreamLogger(stream)) { streamLogger.Log(message); } } using (MemoryStream stream = new MemoryStream(bytes, false)) { using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd().Trim('\r', '\n', '\0'); Assert.Equal("[OMG wtf] bbq", result); } } }
static void Main(string[] args) { int index = 0; string fileName = "nexogen." + index + ".log"; FileStream stream = new FileStream(fileName, FileMode.CreateNew); { using (StreamWriter archive = new StreamWriter(stream)) { archive.WriteLine("BAAAANnng!!!"); archive.Close(); } } long length = new FileInfo(fileName).Length; Console.WriteLine(length); Console.ReadKey(); StreamLogger streamLogger = new StreamLogger(stream); streamLogger.Log(LogLevel.Info, "logging"); }
protected override void OnStart(string[] args) { string logDir = ConfigManager.AppSettings["LogDirectory"].ToString(); string logFileName = logDir + "ServiceAgentLog_" + DateTime.Now.ToString("MMMMddyyyyhhmmss") + ".xml"; FileStream fs = new FileStream(logFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamLogger logStream = new StreamLogger(fs, true); logStream.IsIDGenerationEnabled = true; logStream.IsLineBreakingEnabled = true; logStream.IsPreambleEnabled = true; logStream.IsExceptionAutoFlushEnabled = true; logStream.IsContextEnabled = true; logStream.Open(); try { try { logStream.Log("Service started successfully..."); //retry the action if it fails to start the thread every 30 seconds. //suspected to throw exception during starting the service if the internet is not yet ready. RetryAction(() => Start(), 1000 * 30); } catch (Exception tex) { logStream.Log("SSISPackageExecution using Windows Azure Message Queue"); logStream.Log(String.Format("Error in starting service: {0}", tex.Message + Environment.NewLine + tex.StackTrace)); if (tex.InnerException != null) { logStream.Log("Inner Exception: " + tex.InnerException.Message.ToString()); } } } catch (Exception ex) { logStream.Log(ex); } finally { logStream.Close(); } }