logo
Tags down

shadow

Accessing ILogger from non-Controller classes in Class Libary .NET Core 3


By : Zhu
Date : September 12 2020, 07:00 AM
Hope this helps Instead of instantiating a depended class inside your controller, delegate this responsibility to .NET Core inbuilt DI container - This will help you to inject the depended class directly to the required class.
Below code snippet will help you how to use DI in your existing codebase.
code :
 public class Startup
 {
    ...
    public void ConfigureServices(IServiceCollection services)
    {      
      services.AddLogging();
      services.AddTransient<ClassX>();
      services.AddTransient<ClassY>();
      services.AddTransient<ClassZ>();
      services.AddTransient<ClassD>();
      ...
    }
}

public class ControllerB : ControllerBase
{
    private readonly ClassD classD;
    private readonly ILogger logger;

    public ControllerB(ClassD classD, ILogger<ControllerB> logger)
    {
        this.classD = classD;
        this.logger = logger;
    }
    ...
}

public class ClassD
{
   private readonly ClassZ classZ;

   public ClassD(ClassZ classZ)
   {
       this.classZ = classZ;
   }
} //Do the same thing for ClassZ and ClassY

public class ClassX
{
   private readonly ILogger logger;

   public ClassX(ILogger<ClassX> logger)
   {
       this.logger = logger;
   }
}


Share : facebook icon twitter icon

Inject Serilog's ILogger interface in ASP .NET Core Web API Controller


By : user3040396
Date : March 29 2020, 07:55 AM
I wish this helpful for you If you prefer ILogger instead of ILogger, you could try to register ILogger.
Here are two options to use Serialog.Information.
code :
Log.Logger.Information("Information Log from Log.Logger");
//Startup.cs
services.AddSingleton(Log.Logger);

//Use
public class HomeController : Controller
{
    private readonly ILogger _logger;
    public HomeController(ILogger logger)
    {
        _logger = logger;
    }
    public IActionResult Index()
    {
        _logger.Information("Inform ILog from ILogger");
        return View();
    }        
}

Using the preview versions of asp.net core mvc in a class libary


By : carboleda
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further In ASP.NET Core 3.0, MVC is part of the Shared Framework. The Microsoft.AspNetCore.Mvc package is one of many that will no longer be published separately.

Azure function 2.x ILogger is not competible with .net core ILogger?


By : hex90
Date : March 29 2020, 07:55 AM
this one helps. I have been trying to refer a .net core library project into my Azure function project to call one of the process defined in a .net core class library. , Update:
Have use .net core 3.0 now.
code :
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="librdkafka.redist" Version="1.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="librdkafka.redist" Version="1.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Correctly Injecting Serilog into .net core classes as Microsoft.Extentions.Logging.ILogger - Not ASP .Net Core


By : user3573036
Date : March 29 2020, 07:55 AM
To fix this issue You need to wrap the Serilog logger into Microsoft.Extensions.Logging.LoggerFactory. This is exactly what happens when same is used in DI in ASP.NET.
Like this :
code :
Serilog.Core.Logger serilog = ...;

var loggerFactory = new LoggerFactory()
    .AddSerilog(serilog);

Microsoft.Extensions.Logging.ILogger logger = loggerFactory.CreateLogger("Logger");

How do I get my custom ILogger implementation from ILogger in, for example a controller?


By : Dusty Carver
Date : March 29 2020, 07:55 AM
seems to work fine So BeginScope allows you to provide a context for the log... This link explains it quite nicely... https://nblumhardt.com/2016/11/ilogger-beginscope/
but an example that I did was:
Related Posts Related Posts :
  • SQL Command Parameter cannot covert string to int
  • Azure Notification Hub example to register a device and receive a notification
  • Disabed element shown as enabled in Selenium C#
  • Html.listboxfor() how to capture event on change/add item - Jquery MVC (C#)
  • c# Accessing my array inside a class constructor
  • How to first 'Split a string to an Array' then 'Add something to that Array'? || C# Console App
  • IEnumerable of Tuple of string of string
  • Why don't I get a warning about possible dereference of a null in C# 8 with a class member of a struct?
  • Visual Studio 2019: how to disable specific CodeAnalysis messages in .editorconfig?
  • Pass ID to controller on prev or next
  • What are the difference using app.run and app.useendpoints in asp.net-core?
  • Why string Method(Object object) may not match Func<Object, string> delegate type?
  • Check inside loop if *txt file has been created
  • C# retry logic While loop
  • Convert object {object[]} to string[]
  • Difference between initialize a string to "" or to "".ToString() - Which one should I use?
  • How to make the console wait but then continue without pressing a Key?
  • extract text in rich text format from range
  • how to handle 1000 concurrent user requests per second in asp.net web api
  • In C# How to get Windows Credentials from credential manager
  • Override default value of null conditional operator
  • How to fix override ToString method
  • System.IO.FileNotFoundException when creating EF migrations on .net core
  • Login limit attempts in C#
  • Loop into ListView elements
  • Changing Blazor folder gives me "Cannot find the fallback endpoint"
  • creating class from JSON with different types of a node
  • How to define models for Database tables in C# without using Linq To DB?
  • Understanding Identityserver4 with Identity (cookies/tokens, server architecture)
  • How to Deserialize a JSON into a list using SIMPLE JSON?
  • another option instead using AsEnumerable() in linq EF, when using custom method in linq
  • Parameterized query that returns TEXT column(s) always returns zero for INT columns
  • Why format is not working while appending zeros?
  • SqlConnection string
  • Entity Framework Core Update Database - Code First Approach Without Migration
  • Unity Update Method
  • Custom Middleware is causing Blazor Server-side page to stop working
  • Declaring hex number: The name 'B9780' does not exist in the current context
  • Invalid cast from 'System.Int32' to Enum using reflections
  • Why does my webrequest line need updating? (error (426) Upgrade Required )
  • How to generate Key and Key IV aes in C#
  • How can I display view inside content control in wpf
  • How can I wait for form2 to finish?
  • Is Application.DoEvents() a form of Multitasking?
  • In C#, how can I see if the first character in an account is a specific value?
  • C# Determine if a char at index is between two characters in a string
  • C# - Winform Timer - Disposing and emptying the timer
  • if else condition in Regex c# and return constant value
  • Ternary operator for 3 conditions
  • Fineuploader with Azure, Empty image problem
  • How do I unit test the event raised by class under test?
  • How to set up .net core service that uses a generic repository
  • ASP.NET Core 3.0 Endpoint Routing doesn't work for default route
  • use gmail API token on localhost and hosting at the same time
  • Try-catch FormatException
  • why do sql procedure retuns 0 rows when called from applciation?
  • .Net core 3: The namespace internal does not exist in Microsoft.Extensions.Logging.Internal
  • how to interpolate a variable into string method
  • I want to instantiate an object at the location of another object which is destroyed
  • How to display full path of ALL currently running process in C#?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk