C#日志记录设计与实现(BenXHLog)
C#日志记录设计与实现
日志记录:
日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和实现,如有不足,还望见谅!废话不多,直入主题。
笨小孩日志:BenXHLog
类文件设计:
文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,
程序代码:
Ilog接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace BenXH.Log.Log 7 { 8 public interface ILog 9 { 10 bool IsDebugEnabled { get; } 11 12 bool IsErrorEnabled { get; } 13 14 bool IsFatalEnabled { get; } 15 16 bool IsInfoEnabled { get; } 17 18 bool IsWarnEnabled { get; } 19 20 void Debug(bool isWriteFile,object message); 21 22 void Debug(bool isWriteFile, object message, Exception exception); 23 24 void DebugFormat(bool isWriteFile, string format, object arg0); 25 26 void DebugFormat(bool isWriteFile, string format, params object[] args); 27 28 void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args); 29 30 void DebugFormat(bool isWriteFile, string format, object arg0, object arg1); 31 32 void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); 33 34 void Error(bool isWriteFile,object message); 35 36 void Error(bool isWriteFile, object message, Exception exception); 37 38 void ErrorFormat(bool isWriteFile, string format, object arg0); 39 40 void ErrorFormat(bool isWriteFile, string format, params object[] args); 41 42 void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args); 43 44 void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1); 45 46 void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); 47 48 void Fatal(bool isWriteFile, object message); 49 50 void Fatal(bool isWriteFile, object message, Exception exception); 51 52 void FatalFormat(bool isWriteFile, string format, object arg0); 53 54 void FatalFormat(bool isWriteFile, string format, params object[] args); 55 56 void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args); 57 58 void FatalFormat(bool isWriteFile, string format, object arg0, object arg1); 59 60 void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); 61 62 void Info(bool isWriteFile, object message); 63 64 void Info(bool isWriteFile, object message, Exception exception); 65 66 void InfoFormat(bool isWriteFile, string format, object arg0); 67 68 void InfoFormat(bool isWriteFile, string format, params object[] args); 69 70 void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args); 71 72 void InfoFormat(bool isWriteFile, string format, object arg0, object arg1); 73 74 void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); 75 76 void Warn(bool isWriteFile, object message); 77 78 void Warn(bool isWriteFile, object message, Exception exception); 79 80 void WarnFormat(bool isWriteFile, string format, object arg0); 81 82 void WarnFormat(bool isWriteFile, string format, params object[] args); 83 84 void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args); 85 86 void WarnFormat(bool isWriteFile, string format, object arg0, object arg1); 87 88 void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); 89 } 90 }
ILogFactory工厂接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BenXH.Log.Log { public interface ILogFactory { ILog GetLog(string name); } }
日志类Log 这个代码实现多一点,合并了,点开看吧
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace BenXH.Log.Log 7 { 8 ///9 /// Console Log 10 /// 11 internal class Log : ILog 12 { 13 private string m_Name; 14 15 private const string m_MessageTemplate = "{0}-{1}: {2}"; 16 17 private const string m_Debug = "DEBUG"; 18 19 private const string m_Error = "ERROR"; 20 21 private const string m_Fatal = "FATAL"; 22 23 private const string m_Info = "INFO"; 24 25 private const string m_Warn = "WARN"; 26 27 /// 28 /// Initializes a new instance of the class. 29 /// 30 /// The name. 31 public Log(string name) 32 { 33 m_Name = name; 34 } 35 36 /// 37 /// Gets a value indicating whether this instance is debug enabled. 38 /// 39 /// 40 /// true if this instance is debug enabled; otherwise, false. 41 /// 42 public bool IsDebugEnabled 43 { 44 get { return true; } 45 } 46 47 /// 48 /// Gets a value indicating whether this instance is error enabled. 49 /// 50 /// 51 /// true if this instance is error enabled; otherwise, false. 52 /// 53 public bool IsErrorEnabled 54 { 55 get { return true; } 56 } 57 58 /// 59 /// Gets a value indicating whether this instance is fatal enabled. 60 /// 61 /// 62 /// true if this instance is fatal enabled; otherwise, false. 63 /// 64 public bool IsFatalEnabled 65 { 66 get { return true; } 67 } 68 69 /// 70 /// Gets a value indicating whether this instance is info enabled. 71 /// 72 /// 73 /// true if this instance is info enabled; otherwise, false. 74 /// 75 public bool IsInfoEnabled 76 { 77 get { return true; } 78 } 79 80 /// 81 /// Gets a value indicating whether this instance is warn enabled. 82 /// 83 /// 84 /// true if this instance is warn enabled; otherwise, false. 85 /// 86 public bool IsWarnEnabled 87 { 88 get { return true; } 89 } 90 91 public string GetDataTimeLog(string log) 92 { 93 return String.Format("[{0}]>>{1}", DateTime.Now.ToString("yy-MM-dd HH:mm:ss"), log); 94 } 95 96 /// 97 /// Logs the debug message. 98 /// 99 /// 100 /// The message. 101 public void Debug(bool isWriteFile, object message) 102 { 103 string log = GetDataTimeLog(message.ToString()); 104 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); 105 if (isWriteFile) 106 { 107 LogUtil.WriteLogFile(m_Name, m_Debug, log); 108 } 109 } 110 111 /// 112 /// Logs the debug message. 113 /// 114 /// 115 /// The message. 116 /// The exception. 117 public void Debug(bool isWriteFile, object message, Exception exception) 118 { 119 string log =GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); 120 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); 121 if (isWriteFile) 122 { 123 LogUtil.WriteLogFile(m_Name, m_Debug, log); 124 } 125 } 126 127 /// 128 /// Logs the debug message. 129 /// 130 /// 131 /// The format. 132 /// The arg0. 133 public void DebugFormat(bool isWriteFile, string format, object arg0) 134 { 135 string log = GetDataTimeLog(string.Format(format, arg0)); 136 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); 137 if (isWriteFile) 138 { 139 LogUtil.WriteLogFile(m_Name, m_Debug, log); 140 } 141 } 142 143 /// 144 /// Logs the debug message. 145 /// 146 /// 147 /// The format. 148 /// The args. 149 public void DebugFormat(bool isWriteFile, string format, params object[] args) 150 { 151 string log = GetDataTimeLog(string.Format(format, args)); 152 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(format, args)); 153 if (isWriteFile) 154 { 155 LogUtil.WriteLogFile(m_Name, m_Debug, log); 156 } 157 } 158 159 /// 160 /// Logs the debug message. 161 /// 162 /// 163 /// The provider. 164 /// The format. 165 /// The args. 166 public void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) 167 { 168 string log = GetDataTimeLog(string.Format(format, args)); 169 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(provider, format, args)); 170 if (isWriteFile) 171 { 172 LogUtil.WriteLogFile(m_Name, m_Debug, log); 173 } 174 } 175 176 /// 177 /// Logs the debug message. 178 /// 179 /// 180 /// The format. 181 /// The arg0. 182 /// The arg1. 183 public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1) 184 { 185 string log = GetDataTimeLog(string.Format(format, arg0, arg1)); 186 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); 187 if (isWriteFile) 188 { 189 LogUtil.WriteLogFile(m_Name, m_Debug, log); 190 } 191 } 192 193 /// 194 /// Logs the debug message. 195 /// 196 /// 197 /// The format. 198 /// The arg0. 199 /// The arg1. 200 /// The arg2. 201 public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) 202 { 203 string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); 204 Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); 205 if (isWriteFile) 206 { 207 LogUtil.WriteLogFile(m_Name, m_Debug, log); 208 } 209 } 210 211 /// 212 /// Logs the error message. 213 /// 214 /// 215 /// The message. 216 public void Error(bool isWriteFile, object message) 217 { 218 string log = GetDataTimeLog(message.ToString()); 219 Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); 220 if (isWriteFile) 221 { 222 LogUtil.WriteLogFile(m_Name, m_Error, log); 223 } 224 } 225 226 /// 227 /// Logs the error message. 228 /// 229 /// 230 /// The message. 231 /// The exception. 232 public void Error(bool isWriteFile, object message, Exception exception) 233 { 234 string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); 235 Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); 236 if (isWriteFile) 237 { 238 LogUtil.WriteLogFile(m_Name, m_Error,log); 239 } 240 } 241 242 /// 243 /// Logs the error message. 244 /// 245 /// 246 /// The format. 247 /// The arg0. 248 public void ErrorFormat(bool isWriteFile, string format, object arg0) 249 { 250 string log = GetDataTimeLog(string.Format(format, arg0)); 251 Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); 252 if (isWriteFile) 253 { 254 LogUtil.WriteLogFile(m_Name, m_Error, log); 255 } 256 } 257 258 /// 259 /// Logs the error message. 260 /// 261 /// 262 /// The format. 263 /// The args. 264 public void ErrorFormat(bool isWriteFile, string format, params object[] args) 265 { 266 string log = GetDataTimeLog(string.Format(format, args)); 267 Console.WriteLine(m_MessageTemplate, m_Name, m_Error,log); 268 if (isWriteFile) 269 { 270 LogUtil.WriteLogFile(m_Name, m_Error, log); 271 } 272 } 273 274 /// 275 /// Logs the error message. 276 /// 277 /// 278 /// The provider. 279 /// The format. 280 /// The args. 281 public void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) 282 { 283 string log = GetDataTimeLog(string.Format(provider, format, args)); 284 Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); 285 if (isWriteFile) 286 { 287 LogUtil.WriteLogFile(m_Name, m_Error, log); 288 } 289 } 290 291 /// 292 /// Logs the error message. 293 /// 294 /// 295 /// The format. 296 /// The arg0. 297 /// The arg1. 298 public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1) 299 { 300 string log = GetDataTimeLog(string.Format(format, arg0, arg1)); 301 Console.WriteLine(m_MessageTemplate, m_Name, m_Error,log); 302 if (isWriteFile) 303 { 304 LogUtil.WriteLogFile(m_Name, m_Error, log); 305 } 306 } 307 308 /// 309 /// Logs the error message. 310 /// 311 /// 312 /// The format. 313 /// The arg0. 314 /// The arg1. 315 /// The arg2. 316 public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) 317 { 318 string log = GetDataTimeLog(string.Format(format, arg0, arg2)); 319 Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); 320 if (isWriteFile) 321 { 322 LogUtil.WriteLogFile(m_Name, m_Error, log); 323 } 324 } 325 326 /// 327 /// Logs the fatal error message. 328 /// 329 /// 330 /// The message. 331 public void Fatal(bool isWriteFile, object message) 332 { 333 string log = GetDataTimeLog(message.ToString()); 334 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 335 if (isWriteFile) 336 { 337 LogUtil.WriteLogFile(m_Name,m_Fatal,log); 338 } 339 } 340 341 /// 342 /// Logs the fatal error message. 343 /// 344 /// 345 /// The message. 346 /// The exception. 347 public void Fatal(bool isWriteFile, object message, Exception exception) 348 { 349 string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); 350 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 351 if (isWriteFile) 352 { 353 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 354 } 355 } 356 357 /// 358 /// Logs the fatal error message. 359 /// 360 /// 361 /// The format. 362 /// The arg0. 363 public void FatalFormat(bool isWriteFile, string format, object arg0) 364 { 365 string log = GetDataTimeLog(string.Format(format, arg0)); 366 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 367 if (isWriteFile) 368 { 369 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 370 } 371 } 372 373 /// 374 /// Logs the fatal error message. 375 /// 376 /// 377 /// The format. 378 /// The args. 379 public void FatalFormat(bool isWriteFile, string format, params object[] args) 380 { 381 string log = GetDataTimeLog(string.Format(format, args)); 382 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal,log); 383 if (isWriteFile) 384 { 385 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 386 } 387 } 388 389 /// 390 /// Logs the fatal error message. 391 /// 392 /// 393 /// The provider. 394 /// The format. 395 /// The args. 396 public void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) 397 { 398 string log = GetDataTimeLog(string.Format(provider, format, args)); 399 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 400 if (isWriteFile) 401 { 402 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 403 } 404 } 405 406 /// 407 /// Logs the fatal error message. 408 /// 409 /// 410 /// The format. 411 /// The arg0. 412 /// The arg1. 413 public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1) 414 { 415 string log = GetDataTimeLog(string.Format(format, arg0, arg1)); 416 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 417 if (isWriteFile) 418 { 419 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 420 } 421 } 422 423 /// 424 /// Logs the fatal error message. 425 /// 426 /// 427 /// The format. 428 /// The arg0. 429 /// The arg1. 430 /// The arg2. 431 public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) 432 { 433 string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); 434 Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); 435 if (isWriteFile) 436 { 437 LogUtil.WriteLogFile(m_Name, m_Fatal, log); 438 } 439 } 440 441 /// 442 /// Logs the info message. 443 /// 444 /// 445 /// The message. 446 public void Info(bool isWriteFile,object message) 447 { 448 string log = GetDataTimeLog(message.ToString()); 449 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 450 if (isWriteFile) 451 { 452 LogUtil.WriteLogFile(m_Name,m_Info,log); 453 } 454 } 455 456 /// 457 /// Logs the info message. 458 /// 459 /// 460 /// The message. 461 /// The exception. 462 public void Info(bool isWriteFile, object message, Exception exception) 463 { 464 string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); 465 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 466 if (isWriteFile) 467 { 468 LogUtil.WriteLogFile(m_Name, m_Info, log); 469 } 470 } 471 472 /// 473 /// Logs the info message. 474 /// 475 /// 476 /// The format. 477 /// The arg0. 478 public void InfoFormat(bool isWriteFile, string format, object arg0) 479 { 480 string log = GetDataTimeLog(string.Format(format, arg0)); 481 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 482 if (isWriteFile) 483 { 484 LogUtil.WriteLogFile(m_Name, m_Info, log); 485 } 486 } 487 488 /// 489 /// Logs the info message. 490 /// 491 /// 492 /// The format. 493 /// The args. 494 public void InfoFormat(bool isWriteFile, string format, params object[] args) 495 { 496 string log = GetDataTimeLog(string.Format(format, args)); 497 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 498 if (isWriteFile) 499 { 500 LogUtil.WriteLogFile(m_Name, m_Info, log); 501 } 502 } 503 504 /// 505 /// Logs the info message. 506 /// 507 /// 508 /// The provider. 509 /// The format. 510 /// The args. 511 public void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) 512 { 513 string log = GetDataTimeLog(string.Format(provider, format, args)); 514 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 515 if (isWriteFile) 516 { 517 LogUtil.WriteLogFile(m_Name, m_Info, log); 518 } 519 } 520 521 /// 522 /// Logs the info message. 523 /// 524 /// 525 /// The format. 526 /// The arg0. 527 /// The arg1. 528 public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1) 529 { 530 string log = GetDataTimeLog(string.Format(format, arg0, arg1)); 531 Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); 532 if (isWriteFile) 533 { 534 LogUtil.WriteLogFile(m_Name, m_Info, log); 535 } 536 } 537 538 /// 539 /// Logs the info message. 540 /// 541 /// 542 /// The format. 543 /// The arg0. 544 /// The arg1. 545 /// The arg2. 546 public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) 547 { 548 string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); 549 Console.WriteLine(m_MessageTemplate, m_Name, m_Info,log); 550 if (isWriteFile) 551 { 552 LogUtil.WriteLogFile(m_Name, m_Info, log); 553 } 554 } 555 556 /// 557 /// Logs the warning message. 558 /// 559 /// 560 /// The message. 561 public void Warn(bool isWriteFile,object message) 562 { 563 string log = GetDataTimeLog(message.ToString()); 564 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 565 if (isWriteFile) 566 { 567 LogUtil.WriteLogFile(m_Name,m_Warn,log); 568 } 569 } 570 571 /// 572 /// Logs the warning message. 573 /// 574 /// 575 /// The message. 576 /// The exception. 577 public void Warn(bool isWriteFile, object message, Exception exception) 578 { 579 string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); 580 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 581 if (isWriteFile) 582 { 583 LogUtil.WriteLogFile(m_Name, m_Warn, log); 584 } 585 } 586 587 /// 588 /// Logs the warning message. 589 /// 590 /// 591 /// The format. 592 /// The arg0. 593 public void WarnFormat(bool isWriteFile, string format, object arg0) 594 { 595 string log = GetDataTimeLog(string.Format(format, arg0)); 596 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 597 if (isWriteFile) 598 { 599 LogUtil.WriteLogFile(m_Name, m_Warn, log); 600 } 601 } 602 603 /// 604 /// Logs the warning message. 605 /// 606 /// 607 /// The format. 608 /// The args. 609 public void WarnFormat(bool isWriteFile, string format, params object[] args) 610 { 611 string log = GetDataTimeLog(string.Format(format, args)); 612 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 613 if (isWriteFile) 614 { 615 LogUtil.WriteLogFile(m_Name, m_Warn, log); 616 } 617 } 618 619 /// 620 /// Logs the warning message. 621 /// 622 /// 623 /// The provider. 624 /// The format. 625 /// The args. 626 public void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) 627 { 628 string log = GetDataTimeLog(string.Format(provider, format, args)); 629 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 630 if (isWriteFile) 631 { 632 LogUtil.WriteLogFile(m_Name, m_Warn, log); 633 } 634 } 635 636 /// 637 /// Logs the warning message. 638 /// 639 /// 640 /// The format. 641 /// The arg0. 642 /// The arg1. 643 public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1) 644 { 645 string log = GetDataTimeLog(string.Format(format, arg0, arg1)); 646 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 647 if (isWriteFile) 648 { 649 LogUtil.WriteLogFile(m_Name, m_Warn, log); 650 } 651 } 652 653 /// 654 /// Logs the warning message. 655 /// 656 /// 657 /// The format. 658 /// The arg0. 659 /// The arg1. 660 /// The arg2. 661 public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) 662 { 663 string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); 664 Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); 665 if (isWriteFile) 666 { 667 LogUtil.WriteLogFile(m_Name, m_Warn, log); 668 } 669 } 670 } 671 }
LogFactory 日志工厂
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BenXH.Log.Log { public class LogFactory:ILogFactory { ////// 创建日志实例 /// /// /// public ILog GetLog(string name) { return new Log(name); } } }
LogUtil日志格式化
1 using BenXH.Log.Common; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 7 namespace BenXH.Log.Log 8 { 9 internal class LogUtil 10 { 11 ///12 /// 格式式化Log信息 13 /// 14 /// 15 /// 16 /// 17 /// 18 /// 19 private static string GetLogString(string name, string logType, string log) 20 { 21 return String.Format("[{0}]{1}-{2}: {3}", DateTime.Now.ToString("HH:mm:ss"),name, logType, log); 22 } 23 24 /// 25 /// 获得日志要保存的路径 26 /// 27 /// 28 /// 29 /// 30 private static string GetLogPath(string name, string logType) 31 { 32 string path = AppDomain.CurrentDomain.BaseDirectory+"Log"; 33 if (!System.IO.Directory.Exists(path)) 34 { 35 System.IO.Directory.CreateDirectory(path); 36 } 37 38 return System.IO.Path.Combine(path,String.Format("{0}_{1}_{2}.log",DateTime.Now.ToString("yyyy-MM-dd"),name,logType)); 39 } 40 41 public static void WriteLogFile(string name, string logType, string log) 42 { 43 string logPath = GetLogPath(name, logType); 44 45 FileUtil.WriteAppend(logPath,log); 46 } 47 } 48 }
最后就是一个日志信息的显示和文件的存储 FileUtil
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 7 namespace BenXH.Log.Common 8 { 9 public static class FileUtil 10 { 11 ///12 /// 追加内容到指定文件中 13 /// 14 /// 15 /// 16 public static void WriteAppend(string filePath, string content) 17 { 18 WriteAppend(filePath,new string[]{content}); 19 } 20 21 public static void WriteAppend(string filePath, string[] contents) 22 { 23 //System.IO.StreamWriter sr = new System.IO.StreamWriter(filePath, true); 24 //foreach (string c in contents) 25 //{ 26 // sr.WriteLine(c); 27 //} 28 //sr.Flush(); 29 //sr.Close(); 30 31 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) 32 { 33 fs.Seek(fs.Length, SeekOrigin.Current); 34 35 string content = String.Join(Environment.NewLine, contents) + Environment.NewLine; 36 37 byte[] data = System.Text.Encoding.UTF8.GetBytes(content); 38 39 fs.Write(data, 0, data.Length); 40 41 fs.Close(); 42 } 43 } 44 } 45 }
测试代码:
Test.cs
1 class Test 2 { 3 static void Main(string[] args) 4 { 5 //日志BenXH的Debug信息 6 new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "Hello"); 7 new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "World"); 8 9 //日志BenXH的info信息 10 new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "Hello"); 11 new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "BenXH"); 12 Console.Read(); 13 } 14 }
运行结果:
文件:程序运行根目录下Log文件夹
Log文件夹下新创建俩个文件DEBUG和INFO
文件:2017-07-29_BenXH_DEBUG.log
文件内容
[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>World
文件:2017-07-29_BenXH_INFO.log
文件内容:
[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>BenXH