zoukankan      html  css  js  c++  java
  • Log

    1 internal abstract class ILogger {
    2 protected ILogger() {
    3 }
    4
    5 public virtual void Close() {
    6 }
    7
    8 //protected string Time() {
    9 // TimeSpan span = (TimeSpan)(DateTime.Now - Application.ProgramStartTime);
    10 // return (span.Hours.ToString().PadLeft(2, '0') + ":" + span.Minutes.ToString().PadLeft(2, '0') + ":" + span.Seconds.ToString().PadLeft(2, '0') + "." + span.Milliseconds.ToString().PadLeft(3, '0'));
    11 //}
    12
    13 public abstract void Write(params string[] msg);
    14
    15 public virtual void WriteRaw(string msg) {
    16 this.Write(new string[] { msg });
    17 }
    18 }
    19
    20 internal abstract class FileLogger : ILogger {
    21 private StreamWriter file;
    22 protected object locker = new object();
    23
    24 public FileLogger(string filename) {
    25 lock (this.locker) {
    26 this.file = new StreamWriter(filename);
    27 this.file.AutoFlush = true;
    28 }
    29 }
    30
    31 public override void Close() {
    32 this.file.Close();
    33 }
    34
    35 public override void WriteRaw(string msg) {
    36 lock (this.locker) {
    37 this.file.Write(msg);
    38 }
    39 }
    40 }
    41
    42 internal class TextLogger : FileLogger {
    43 public TextLogger(string log)
    44 : base(log + ".txt") {
    45 }
    46
    47 public override void Write(params string[] msg) {
    48 lock (base.locker) {
    49 StringBuilder builder = new StringBuilder();
    50 builder.Append(DateTime.Now.ToString()).Append("| ").Append(msg[0]).Append("\r\n");
    51 for (int i = 1; i < msg.Length; i++) {
    52 builder.Append(" | ").Append(msg[i]).Append("\r\n");
    53 }
    54
    55 this.WriteRaw(builder.ToString());
    56 }
    57 }
    58 }
    59
    60 ---------------------------------------------------
    61 public static void LogInit(){
    62 if (!logInited) {
    63 logInited = true;
    64 if (!Directory.Exists(ApplicationDataFolder + "Logs")) {
    65 Directory.CreateDirectory(ApplicationDataFolder + "Logs");
    66 }
    67
    68 applicationLog = new TextLogger(ApplicationDataFolder + "Logs/ApplicationLog");
    69 applicationLog.Write(new string[] { "Date: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
    70 applicationLog.Write(new string[] { "assembly version: " + typeof(App).Assembly.GetName() });
    71 applicationLog.Write(new string[] { "PreInit log: " + DateTime.Now.ToString("HH:mm:ss") });
    72 applicationLog.Write(new string[] { preInitLog.ToString() });
    73 }
    74 }
    75
    76 public static void Log(params string[] msg) {
    77 if (!logInited) {
    78 preInitLog.Append("PreInit[" + DateTime.Now.ToString("HH:mm:ss") + "]|");
    79 foreach (string str in msg) {
    80 preInitLog.Append(str + "\r\n");
    81 }
    82 } else {
    83 applicationLog.Write(msg);
    84 }
    85 }
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/anduinlothar/p/2083256.html
Copyright © 2011-2022 走看看