本文整理汇总了C#中LogSource.TraceData方法的典型用法代码示例。如果您正苦于以下问题:C# LogSource.TraceData方法的具体用法?C# LogSource.TraceData怎么用?C# LogSource.TraceData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LogSource
的用法示例。
在下文中一共展示了LogSource.TraceData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendLogEntry
void SendLogEntry(WmiTraceListener listener,
LogEntry logEntry)
{
ManagementScope scope = new ManagementScope(@"\\." + wmiPath);
scope.Options.EnablePrivileges = true;
StringBuilder sb = new StringBuilder("SELECT * FROM ");
sb.Append("LogEntryV20");
string query = sb.ToString();
EventQuery eq = new EventQuery(query);
using (ManagementEventWatcher watcher = new ManagementEventWatcher(scope, eq))
{
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 1, logEntry);
BlockUntilWMIEventArrives();
watcher.Stop();
}
}
示例2: FlatFileListenerWillFallbackIfNotPriviledgesToWrite
public void FlatFileListenerWillFallbackIfNotPriviledgesToWrite()
{
string fileName = @"trace.log";
string fullPath = String.Format(@"{0}\{1}", Directory.GetCurrentDirectory(), fileName);
File.Delete(fileName);
FileIOPermission fileIOPerm1 = new FileIOPermission(PermissionState.None);
fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, fullPath);
fileIOPerm1.PermitOnly();
try
{
FlatFileTraceListener listener = new FlatFileTraceListener(fileName, "---header---", "***footer***",
new TextFormatter("DUMMY{newline}DUMMY"));
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", new[] { listener }, SourceLevels.All);
source.TraceData(TraceEventType.Error, 0,
new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
listener.Dispose();
}
catch (SecurityException)
{
FileIOPermission.RevertAll();
throw;
}
}
示例3: FormattedListenerWillUseFormatterIfExists
public void FormattedListenerWillUseFormatterIfExists()
{
StringWriter writer = new StringWriter();
FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", writer.ToString());
}
示例4: ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists
public void ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
{
LogEntry testEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
StringWriter writer = new StringWriter();
FormattedEventLogTraceListener listener = new FormattedEventLogTraceListener(CommonUtil.EventLogSourceName, CommonUtil.EventLogNameCustom, null);
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", new[] { listener }, SourceLevels.All);
source.TraceData(TraceEventType.Error, 1, testEntry);
Assert.AreEqual(testEntry.ToString(), CommonUtil.GetLastEventLogEntryCustom());
}
示例5: LogSourceDoesNotAutoFlush
public void LogSourceDoesNotAutoFlush()
{
MockFlushSensingTraceListener traceListener = new MockFlushSensingTraceListener();
List<TraceListener> listeners = new List<TraceListener>(1);
listeners.Add(traceListener);
LogSource logSource = new LogSource("name", listeners, SourceLevels.All);
logSource.AutoFlush = false;
logSource.TraceData(TraceEventType.Critical, 0, CommonUtil.GetDefaultLogEntry());
Assert.AreEqual(0, traceListener.flushCalls);
}
示例6: ListenerWillUseFormatterIfExists
public void ListenerWillUseFormatterIfExists()
{
StringWriter writer = new StringWriter();
FormattedEventLogTraceListener listener = new FormattedEventLogTraceListener(CommonUtil.EventLogSourceName, CommonUtil.EventLogNameCustom, new TextFormatter("DUMMY{newline}DUMMY"));
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
LogEntry logEntry = CommonUtil.GetDefaultLogEntry();
source.TraceData(TraceEventType.Error, 1, logEntry);
Assert.AreEqual("DUMMY" + Environment.NewLine + "DUMMY", CommonUtil.GetLastEventLogEntryCustom());
}
示例7: ListenerWithHeaderAndFooterWillUseFormatterIfExists
public void ListenerWithHeaderAndFooterWillUseFormatterIfExists()
{
File.Delete("tracewithheaderandfooter.log");
FlatFileTraceListener listener = new FlatFileTraceListener("tracewithheaderandfooter.log", "--------header------", "=======footer===========", new TextFormatter("DUMMY{newline}DUMMY"));
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", new[] { listener }, SourceLevels.All);
source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null));
listener.Dispose();
string strFileContents = GetFileContents("tracewithheaderandfooter.log");
Assert.AreEqual("--------header------" + Environment.NewLine + "DUMMY" + Environment.NewLine + "DUMMY" + Environment.NewLine + "=======footer===========" + Environment.NewLine, strFileContents);
}
示例8: LogSourceCallsFlushRegardlessOfAutoflushValue
public void LogSourceCallsFlushRegardlessOfAutoflushValue()
{
MockFlushSensingTraceListener traceListener = new MockFlushSensingTraceListener();
List<TraceListener> traceListeners = new List<TraceListener>(1);
traceListeners.Add(traceListener);
LogSource logSource = new LogSource("name", traceListeners, SourceLevels.All);
bool currentAutoFlush = Trace.AutoFlush;
try
{
Trace.AutoFlush = false;
logSource.TraceData(TraceEventType.Critical, 0, CommonUtil.GetDefaultLogEntry());
Trace.AutoFlush = true;
logSource.TraceData(TraceEventType.Critical, 0, CommonUtil.GetDefaultLogEntry());
Assert.AreEqual(2, traceListener.flushCalls);
}
finally
{
Trace.AutoFlush = currentAutoFlush;
}
}
示例9: FormattedListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists
public void FormattedListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
{
LogEntry testEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
StringWriter writer = new StringWriter();
FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer);
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 0, testEntry);
string writtenData = writer.ToString();
string testEntryToString = testEntry.ToString();
Assert.IsTrue(-1 != writtenData.IndexOf(testEntryToString));
}
示例10: ShouldLogApplyingLog
public void ShouldLogApplyingLog()
{
using (StringWriter writer = new StringWriter())
{
FormattedTextWriterTraceListener listener = new FormattedTextWriterTraceListener(writer, new TextFormatter("DUMMY{newline}DUMMY"));
listener.Filter = new EventTypeFilter(SourceLevels.Error);
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Critical, "title", null));
Assert.AreNotEqual(0, writer.ToString().Length);
}
}
示例11: ListenerWillUseFormatterIfExists
public void ListenerWillUseFormatterIfExists()
{
MockEmailTraceListener listener = new MockEmailTraceListener(new TextFormatter("DUMMY\r\nTime:{timestamp}\r\nMessage:{message}DUMMY"));
LogEntry entry = new LogEntry("Test Message", "Test Category", 42, 999, TraceEventType.Information, "Test Title", null);
DateTime messageTimestamp = DateTime.UtcNow;
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", new[] { listener }, SourceLevels.All);
source.TraceData(TraceEventType.Error, 0, entry);
Assert.AreEqual("EntLib-Logging: Information has occurred", lastMailMessageSent.Subject);
Assert.AreEqual("[email protected]", lastMailMessageSent.To[0].Address);
Assert.AreEqual("[email protected]", lastMailMessageSent.To[1].Address);
Assert.AreEqual("[email protected]", lastMailMessageSent.From.Address);
AssertContainsSubstring(lastMailMessageSent.Body, messageTimestamp.ToString());
}
示例12: ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists
public void ListenerWillFallbackToTraceEntryToStringIfFormatterDoesNotExists()
{
LogEntry testLogEntry = new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null);
StreamWriter writer = new StreamWriter("trace.log");
FlatFileTraceListener listener = new FlatFileTraceListener(writer);
// need to go through the source to get a TraceEventCache
LogSource source = new LogSource("notfromconfig", new[] { listener }, SourceLevels.All);
source.TraceData(TraceEventType.Error, 0, testLogEntry);
listener.Dispose();
string strFileContents = GetFileContents("trace.log");
string testLogEntryAsString = testLogEntry.ToString();
Assert.IsTrue(-1 != strFileContents.IndexOf(testLogEntryAsString));
}
示例13: ListenerWillFallbackToTraceEntryMessageIfFormatterDoesNotExists
public void ListenerWillFallbackToTraceEntryMessageIfFormatterDoesNotExists()
{
MockEmailTraceListener listener = new MockEmailTraceListener("[email protected];[email protected]", "[email protected]",
"EntLib-Logging ->", "has occurred", "smtphost");
LogEntry entry = new LogEntry("Test Message", "Test Category", 42, 999, TraceEventType.Error, "Test Title", null);
DateTime messageTimestamp = DateTime.UtcNow;
LogSource source = new LogSource("notfromconfig", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 0, entry);
Assert.AreEqual("EntLib-Logging -> Error has occurred", lastMailMessageSent.Subject);
Assert.AreEqual("[email protected]", lastMailMessageSent.To[0].Address);
Assert.AreEqual("[email protected]", lastMailMessageSent.To[1].Address);
Assert.AreEqual("[email protected]", lastMailMessageSent.From.Address);
AssertContainsSubstring(lastMailMessageSent.Body, "Test Message");
}
示例14: CanSendMessageToQueue
public void CanSendMessageToQueue()
{
ILogFormatter formatter = new BinaryLogFormatter();
MsmqTraceListener listener =
new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());
LogSource source = new LogSource("unnamed", new List<TraceListener>(new TraceListener[] { listener }), SourceLevels.All);
MockMsmqInterface.Instance.transactional = false;
LogEntry entry = CommonUtil.GetDefaultLogEntry();
source.TraceData(TraceEventType.Error, 1, entry);
Message message = MockMsmqInterface.Instance.message;
Assert.IsNotNull(message);
Assert.IsNotNull(message.Body);
Assert.AreEqual(message.Body.GetType(), typeof(string));
Assert.AreEqual(MessageQueueTransactionType.None, MockMsmqInterface.Instance.transactionType);
LogEntry deserializedEntry = BinaryLogFormatter.Deserialize(message.Body as string);
Assert.IsNotNull(deserializedEntry);
}
示例15: ShouldSendMessageToQueueApplyingFilter
public void ShouldSendMessageToQueueApplyingFilter()
{
ILogFormatter formatter = new BinaryLogFormatter();
MsmqTraceListener listener =
new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());
// Filter only Warning
listener.Filter = new EventTypeFilter(SourceLevels.Warning);
LogSource source = new LogSource("unnamed", new List<TraceListener>(new TraceListener[] { listener }), SourceLevels.All);
MockMsmqInterface.Instance.transactional = false;
MockMsmqInterface.Instance.ResetMessageCount();
LogEntry entry = CommonUtil.GetDefaultLogEntry();
// Trace a Critical LogEntry
source.TraceData(TraceEventType.Critical, 1, entry);
Assert.AreEqual(1, MockMsmqInterface.Instance.MessageCount);
}