本文整理汇总了C#中LogMessage.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# LogMessage.ToString方法的具体用法?C# LogMessage.ToString怎么用?C# LogMessage.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LogMessage
的用法示例。
在下文中一共展示了LogMessage.ToString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ItFormatsTimestampsAsExpected
public void ItFormatsTimestampsAsExpected()
{
var logMessage = new LogMessage
{
Message = "Hello, world!",
};
var jsonMessage = logMessage.ToString();
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonMessage);
var regex = new Regex(@"\d+\.\d{9}");
StringAssert.Matches(dictionary["timestamp"], regex);
}
示例2: ItConvertsFieldsToUnderscorized
public void ItConvertsFieldsToUnderscorized()
{
var logMessage = new LogMessage
{
Message = "Hello, world!",
LogLevel = LogLevel.FATAL
};
var jsonMessage = logMessage.ToString();
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonMessage);
Assert.AreEqual("Hello, world!", dictionary["message"]);
Assert.AreEqual(LogLevel.FATAL.ToString("D"), dictionary["log_level"]);
}
示例3: LogMessage
private void LogMessage(LogMessage logMessage)
{
var logTask = new Task(() =>
{
try
{
Logger.Debug(logMessage.ToString());
}
catch (Exception ex)
{
if (ex is AggregateException)
ex = ex.InnerException;
Logger.Error("Erro ao gerar log da resposta: " + ex.Message, ex);
}
});
logTask.Start();
}
示例4: GetFormatted
/// <summary>
/// Возвращет форматированный текст из представления сообщения <see cref="LogMessage"/>
/// </summary>
/// <param name="message">Представление сообщения <see cref="LogMessage"/></param>
/// <returns>Форматированная строка сообщения</returns>
protected virtual string GetFormatted(LogMessage message) {
string text;
if (!string.IsNullOrEmpty(CustomFormat)) {
text = Regex.Replace(CustomFormat, @"\$\{(\w+)\}", m =>
{
var propName = m.Groups[1].Value;
var prop = message.GetType().GetProperty(propName);
if (null != prop) {
var val = prop.GetValue(message, null);
if (null == val) {
return "";
}
if (val is DateTime) {
return ((DateTime) val).ToString("yyyy-MM-dd HH:mm:ss");
}
return val.ToString();
}
return "";
});
}
else {
text = message.ToString();
}
return text;
}
示例5: LogFailSafe
/// <summary>
/// Fail safe UserLog gate to write internal exception without main logging context, but with max guarantee of regestering
/// synchronous
/// </summary>
public void LogFailSafe(LogMessage message) {
if (null == message) {
return;
}
if (string.IsNullOrEmpty(message.Name)) {
message.Name = "FAILSAFE";
}
try {
StreamWriter wr = null;
for (var i = 0; i < 100; i++) {
try {
wr = new StreamWriter(Path.Combine(EnvironmentInfo.RootDirectory, "failsafelog" + i + ".txt"), true, Encoding.UTF8);
break;
}
// ReSharper disable EmptyGeneralCatchClause
catch (Exception) {}
// ReSharper restore EmptyGeneralCatchClause
}
if (null == wr) {
var tmpfile = Path.GetTempFileName() + ".failsafelog." +
DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss", CultureInfo.InvariantCulture) + ".txt";
wr = new StreamWriter(tmpfile, true, Encoding.UTF8);
}
using (wr) {
wr.WriteLine(message.ToString());
wr.Flush();
}
}
catch (Exception ex) {
throw new LogException("even safe mode fails to be executed", ex);
}
}