本文整理汇总了C#中Log.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Log.ToString方法的具体用法?C# Log.ToString怎么用?C# Log.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Log
的用法示例。
在下文中一共展示了Log.ToString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Log
/// <summary>
/// Log a message.
/// </summary>
/// <param name="message">Message to log. </param>
/// <param name="level">Error severity level. </param>
public void Log(Log.Level level, string message)
{
FileStream fileStream = null;
StreamWriter writer = null;
StringBuilder messageBuilder = new StringBuilder();
try
{
fileStream = _logFile.Open(FileMode.OpenOrCreate,
FileAccess.Write, FileShare.Read);
writer = new StreamWriter(fileStream);
// Set the file pointer to the end of the file
writer.BaseStream.Seek(0, SeekOrigin.End);
// Create the message
messageBuilder.Append(System.DateTime.Now.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss"))
.Append(" | ").Append(level.ToString()).Append(" | ").Append(message);
// Force the write to the underlying file
writer.WriteLine(messageBuilder.ToString());
writer.Flush();
}
finally
{
if (writer != null)
writer.Close();
}
}
示例2: WriteMessage
public override void WriteMessage(string message, Log.MessageType type)
{
string newLine = Environment.NewLine;
FileStream fileStream = null;
StreamWriter writer = null;
StringBuilder mess = new StringBuilder();
try
{
fileStream = new FileStream(_fileLocation + this.FileName, FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(fileStream);
lock (SyncObject)
{
writer.BaseStream.Seek(0, SeekOrigin.End);
// [DateTime] [error/warn] [client 127.0.0.1] 200 (code) - "Url" - "Info system"
string banner = string.Format("[" + type.ToString().ToUpper() + "][" + DateTimeLog + "]");
string body = message;
string end = "----------------------------------------------------";
mess.AppendLine(banner + newLine + body + newLine + end);
writer.Write(mess);
}
writer.Flush();
}
finally
{
if (writer != null)
writer.Close();
}
}
示例3: Write
/// <summary>
/// Log message.
/// </summary>
public void Write(Log.Level level, string format, params object[] args)
{
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string message;
if (args.Length > 0)
{
message = string.Format(format, args);
}
else
{
message = format;
}
Write(dt + ' ' + level.ToString() + ' ' + message);
}
示例4: write
void write(Log.MessageType type, string message, string details)
{
lock (this)
{
if (Writing != null)
Writing.Invoke(type, message, details);
if (Log.write_log)
{
if (log_writer == null)
log_writer = new StreamWriter(Path, true);
details = string.IsNullOrWhiteSpace(details) ? "" : "\r\n\r\n" + details;
switch (type)
{
case Log.MessageType.INFORM: message = "INFORM: " + message; break;
case Log.MessageType.WARNING: message = "WARNING: " + message; break;
case Log.MessageType.ERROR:
message = "ERROR: " + message + details;
_ErrorCount++;
break;
case Log.MessageType.EXIT:
message = "EXIT: " + message + details;
_ErrorCount++;
break;
case Log.MessageType.TRACE: message = "TRACE: " + message; break;
case Log.MessageType.LOG: break;
default: throw new Exception("No case for " + type.ToString());
}
if (MaxFileSize > 0)
{
FileInfo fi = new FileInfo(Path);
if (fi.Length > MaxFileSize)
{
log_writer.Close();
int counter = 0;
file_name = Regex.Replace(file_name, @"(\d+_)(\d+)(\.[^\.]+)$",
(Match m) =>
{
counter = int.Parse(m.Groups[2].Value) + 1;
return m.Groups[1].Value + counter + m.Groups[3].Value;
}
);
if (counter < 1)
file_name = Regex.Replace(file_name, @"\.[^\.]+$", @"_1$0");
log_writer = new StreamWriter(Path, true);
}
}
//if (!string.IsNullOrWhiteSpace(details))
// message += "\r\n\r\n" + details;
log_writer.WriteLine(DateTime.Now.ToString("[dd-MM-yy HH:mm:ss] ") + message);
log_writer.Flush();
}
}
}
示例5: SaveLog
public void SaveLog(Log log)
{
this.Save(log.DateTime, log.ToString());
}
示例6: TestTranslations
public void TestTranslations()
{
Log log = new Log(() => Resources.TestString, "p0").Add();
var culture = Resources.Culture;
Resources.Culture = Translation.DefaultCulture;
Trace.WriteLine(log.ToString(Log.VerboseFormat) + Environment.NewLine);
Assert.AreEqual(string.Format(Resources.TestString, "p0"), log.Message);
Resources.Culture = CultureInfo.InvariantCulture;
Trace.WriteLine(log.ToString(Log.VerboseFormat, Resources.Culture) + Environment.NewLine);
Assert.AreEqual(string.Format(Resources.TestString, "p0"), log.GetMessage(Resources.Culture));
Resources.Culture = CultureHelper.GetCultureInfo("fr-FR");
Trace.WriteLine(log.ToString(Log.VerboseFormat, Resources.Culture) + Environment.NewLine);
Assert.AreEqual(string.Format(Resources.TestString, "p0"), log.GetMessage(Resources.Culture));
Resources.Culture = CultureHelper.GetCultureInfo("de-DE");
Trace.WriteLine(log.ToString(Log.VerboseFormat, Resources.Culture) + Environment.NewLine);
Assert.AreEqual(string.Format(Resources.TestString, "p0"), log.GetMessage(Resources.Culture));
Resources.Culture = culture;
}
示例7: TestToFromDictionary
public void TestToFromDictionary()
{
Log initialLog = new Log(_logDictionary);
Trace.WriteLine(initialLog.ToString(Log.AllFormat));
Trace.WriteLine(string.Empty);
Dictionary<string, string> resultDictionary = initialLog.AllProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Log resultLog = new Log(initialLog.AllProperties);
Trace.WriteLine(resultLog.ToString(Log.AllFormat));
Trace.WriteLine(string.Empty);
Log.Flush().Wait();
CollectionAssert.AreEquivalent(_logDictionary, resultDictionary);
}