Skip to main content
Added C# syntax highlighting to code, drastically improving readability (at least for me)
Source Link

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } 
/// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } 

Which you initialize on Startup.cs:

public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE } 
public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper { private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 
internal static class CoreJobSweeper { private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } 

Which you initialize on Startup.cs:

public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper { private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } 

Which you initialize on Startup.cs:

public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper { private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 
improve code formatting
Source Link
spottedmahn
  • 16.2k
  • 21
  • 124
  • 204

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary>  /// Shared logger  /// </summary>  internal static class ApplicationLogging  {   internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory();   internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>();   internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);    } 

Which you intializeinitialize on Startup.cs:

 public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment)  {   _log = logger;   _hostingEnvironment = hostingEnvironment;   Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE    } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper  {   private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary>  /// Shared logger  /// </summary>  internal static class ApplicationLogging  {   internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory();   internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>();   internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);    } 

Which you intialize on Startup.cs:

 public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment)  {   _log = logger;   _hostingEnvironment = hostingEnvironment;   Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE    } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper  {   private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); 

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } 

Which you initialize on Startup.cs:

public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper { private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); } 
Post Undeleted by JohnC
added 5 characters in body
Source Link
JohnC
  • 4.2k
  • 7
  • 45
  • 49

Solution is during Startup.cs put an instance of ServiceProvider into have a static class that can then be usedreference to get the service from anotherLoggerFactory in a utility static class:

Startup.cs initialized on startup:

/// public<summary>  void Configure(IApplicationBuilder app, IHostingEnvironment/// env,Shared logger /// </summary>  Context dbContext,internal ServiceProviderstatic serviceProvider)class ApplicationLogging {  {  internal static ILoggerFactory LoggerFactory { get; set; }//   = new LoggerFactory();  internal static //StoreILogger aCreateLogger<T>() reference=> toLoggerFactory.CreateLogger<T>(); the dependency injection service for static classes internal static ILogger CreateLogger(string ServiceProviderProvidercategoryName) => LoggerFactory.ProviderCreateLogger(categoryName);  = serviceProvider; } 

ServiceProviderProviderWhich you intialize on Startup.cs:

 /// <summary> /// Shared service provider forpublic staticStartup(ILogger<Startup> classeslogger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment)  /// </summary>   {   internal static class ServiceProviderProvider  _log = {logger;   //CALL IT_hostingEnvironment LIKE= THIS:hostingEnvironment;   //  ApiServerState apiServerState = (ApiServerState)ServiceProviderProviderUtil.ProviderApplicationLogging.GetService(typeof(ApiServerState)); LoggerFactory = logFactory;//<===HERE  } 

Then you can build a logger to use from your static class like so:

internal static IServiceProvider Provider { get; set; } class CoreJobSweeper  {        private static } 

Example usage in a static class:

IMetricsILogger metricslog = (IMetrics)ServiceProviderProviderUtil.ProviderApplicationLogging.GetService(typeofCreateLogger(IMetrics)"CoreJobSweeper"); 

Solution is during Startup.cs put an instance of ServiceProvider in a static class that can then be used to get the service from another static class:

Startup.cs:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, Context dbContext, ServiceProvider serviceProvider) {     //Store a reference to the dependency injection service for static classes ServiceProviderProvider.Provider = serviceProvider; 

ServiceProviderProvider.cs

 /// <summary> /// Shared service provider for static classes  /// </summary>      internal static class ServiceProviderProvider  {   //CALL IT LIKE THIS:   //  ApiServerState apiServerState = (ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(ApiServerState));  internal static IServiceProvider Provider { get; set; }        } 

Example usage in a static class:

IMetrics metrics = (IMetrics)ServiceProviderProvider.Provider.GetService(typeof(IMetrics)); 

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup:

/// <summary>  /// Shared logger /// </summary>  internal static class ApplicationLogging {  internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory();  internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);   } 

Which you intialize on Startup.cs:

 public Startup(ILogger<Startup> logger, ILoggerFactory logFactory, IHostingEnvironment hostingEnvironment) { _log = logger; _hostingEnvironment = hostingEnvironment; Util.ApplicationLogging.LoggerFactory = logFactory;//<===HERE  } 

Then you can build a logger to use from your static class like so:

internal static class CoreJobSweeper  {  private static ILogger log = Util.ApplicationLogging.CreateLogger("CoreJobSweeper"); 
Post Deleted by JohnC
Source Link
JohnC
  • 4.2k
  • 7
  • 45
  • 49
Loading