Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2012-2020 fo-dicom contributors.
  2. // Licensed under the Microsoft Public License (MS-PL).
  3. using System;
  4. using Dicom.Log;
  5. using Serilog;
  6. using Serilog.Enrichers;
  7. namespace Dicom.Demo.SerilogDemo
  8. {
  9. internal class Program
  10. {
  11. //Set this to false if Seq (http://getseq.net) is not present
  12. private static bool useSeq = true;
  13. private static void Main(string[] args)
  14. {
  15. //SPECIFIC LOGGER VERSUS GLOBAL LOGGER
  16. //var serilogManager = UseSpecificSerilogLogger();
  17. //ALTERNATE
  18. var serilogManager = UseGlobalSerilogLogger();
  19. // Initialize log manager.
  20. LogManager.SetImplementation(serilogManager);
  21. //Do some DICOM work
  22. var file = DicomFile.Open(@"..\..\..\DICOM Media\Data\Patient1\2.dcm");
  23. //Example of logging a dicom dataset
  24. //file.Dataset.WriteToLog(LogManager.Default.GetLogger("dumpedDataset"), LogLevel.Info);
  25. //Other logging using fo-dicom's log abstraction
  26. Dicom.Log.Logger foDicomLogger = LogManager.GetLogger("testLog");
  27. foDicomLogger.Fatal("A fatal message at {dateTime}", DateTime.Now);
  28. foDicomLogger.Debug("A debug for file {filename} - info: {@metaInfo}", file.File.Name, file.FileMetaInfo);
  29. Console.WriteLine("Finished - hit enter to exit");
  30. Console.ReadLine();
  31. }
  32. private static SerilogManager UseSpecificSerilogLogger()
  33. {
  34. //Get a Serilog logger instance
  35. var logger = ConfigureLogging();
  36. //Wrap it in some extra context as an example
  37. logger = logger.ForContext("Purpose", "Demonstration");
  38. //Configure fo-dicom & Serilog
  39. return new SerilogManager(logger);
  40. }
  41. private static SerilogManager UseGlobalSerilogLogger()
  42. {
  43. //Configure logging
  44. ConfigureLogging();
  45. //Configure fo-dicom & Serilog
  46. return new SerilogManager();
  47. }
  48. /// <summary>
  49. /// Create and return a serilog ILogger instance.
  50. /// For convenience this also sets the global Serilog.Log instance
  51. /// </summary>
  52. /// <returns></returns>
  53. public static ILogger ConfigureLogging()
  54. {
  55. var loggerConfig = new LoggerConfiguration()
  56. //Enrich each log message with the machine name
  57. .Enrich.With<MachineNameEnricher>()
  58. //Accept verbose output (there is effectively no filter)
  59. .MinimumLevel.Verbose()
  60. //Write out to the console using the "Literate" console sink (colours the text based on the logged type)
  61. .WriteTo.LiterateConsole()
  62. //Also write out to a file based on the date and restrict these writes to warnings or worse (warning, error, fatal)
  63. .WriteTo.RollingFile(@"Warnings_{Date}.txt", global::Serilog.Events.LogEventLevel.Warning);
  64. if (useSeq)
  65. {
  66. //Send events to a default installation of Seq on the local computer
  67. loggerConfig = loggerConfig.WriteTo.Seq("http://localhost:5341");
  68. }
  69. var logger = loggerConfig
  70. //Take all of that configuration and make a logger
  71. .CreateLogger();
  72. //Stash the logger in the global Log instance for convenience
  73. global::Serilog.Log.Logger = logger;
  74. return logger;
  75. }
  76. }
  77. }