How does Microsoft.Extensions.Logging work for full .net framework? - asp.net

The article (Monitor and diagnose Azure Service Fabric applications) indicates following (please note text in bold):
ASP.NET Core logging
Choosing how to instrument your code can be difficult, if you chose poorly and have to reinstrument, you are revisiting and potentially destabilizing your code base. To reduce the risk, developers can choose an instrumentation library such as Microsoft.Extensions.Logging provided by ASP.NET Core. This provides an ILogger interface that allows the provider of your choice to be used while minimizing the impact to existing code. Another nice aspect of this is that the code can be used not only in .NET Core on Windows and Linux, but in the full .NET framework too, giving the ability to standardize your instrumentation code across .NET and .NET Core.
How is this supposed to work because when I tried to add the extensions library (to my service fabric cluster application project that compiles to .net framework 4.5.2), it is attempting to bring down all asp.net core related binaries?

#LoekD's answer is absolutely correct. Here's a .NET Framework example of how to use the Microsoft Extentions Logging framework with Serilog.
public class Program
{
private static void Main()
{
// instantiate and configure logging. Using serilog here, to log to console and a text-file.
var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
var loggerConfig = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
loggerFactory.AddSerilog(loggerConfig);
// create logger and put it to work.
var logProvider = loggerFactory.CreateLogger<Program>();
logProvider.LogDebug("debiggung");
}
}
Requires Microsoft.Extensions.Logging, Serilog.Extensions.Logging and Serilog.Sinks.File NuGet packages.

This means that the library 'Microsoft.Extensions.Logging' is compiled against netstandard (1.1), which means it can be used by both full framework (4.5+) applications and dotnet core applications.
Adding the net standard metapackage introduces a bunch of dependencies, but since your project is targeting the full framework, they won't actually be used by your service.

Related

Where is the Program.cs or Startup.cs file in asp.net?

I am trying to do an online library(as in an online book store) using ASP.NET MVC. I can't find the Program.cs file or the Startup.cs file.
I tried manually creating the Program.cs file but when I write the line
var builder = WebApplication.CreateBuilder(args);
it does not find the WebApplication class.
I tried adding
using Microsoft.Graph;
but then I get another error:
WebApplication' does not contain a definition for 'CreateBuilder
What should I do?
Serge made a good point in the comments. Based on your notes, it looks like you have created a .NET 4 project. What you want to do is recreate your project as an ASP.NET Core web app (.NET 5 or higher). Program.cs will then be created automatically.
.NET 4 and 5 are completely different under the hood, so this distinction is important. Rather than being an iteration, the latter was written from the ground up to be more scalable and support concepts like multiple platforms, microservices, and containers.

Migrating a console application to net core

One of my Client wants to migrate a console app (having System.Web dlls) to .Net Core. Do we have any relevant mapping tool which shows corresponding methods of System.Web to .Net Core.
Currently .Net portability analyzer only provides a compatibility report showing whether app can be ported or not, for relevant corresponding method mappings etc. client has to use MS docs and then do relevant method calls for .net core.
Any suggestions or recommendations or any tool which can provide relevant method replacements or make migration easier?.
Thanks In Advance!!!..

Service Fabric V2 remoting Custom Headers

I am using Service Fabric with .net core with Service Fabric Remoting V2. and I am building a multi-tenant app and I want to add custom header to send the tenant ID automatically.
I have started with the solution for this question which uses normal .net framework with service fabric but in the case of .net core it didn't even compile since the service fabric with .net core has different APIs and Methods.
The code goes as follows and There are many cases for non existing APIs:
The Client
ProxyFactory
var _proxyFactory =
new ServiceProxyFactory(c => new ServiceRemotingClientFactoryWrapper(new WcfServiceRemotingClientFactory(callbackClient: c)));
The bold class comes from the Microsoft.ServiceFabric.Services.Wcf package (aside whats said in the nuget site) it is asking me to add a System.ServiceModel Dll which is a full .net library and not even in the stated dependencies.
The ServiceRemotingClientWrapper provided in the solution contains a class named ServiceRemotingMessageHeaders which according to documentation is the in the service remoting class but it doesn't exist.
Also the functions in the interface take IServiceRemotingRequestMessage which is different from the aforementioned demo.
The Server
the class ServiceRemotingDispatcher should be in the remoting Dll and yet it doesn't exit.
Finally, This project uses the same methodology to implement my goal and its based on the same SOF question but uses full .net framework and it works. Link to project

Publish Web API controllers in separate files

I have a problem with publishing Web API in Visual Studio.
In VS, when I publish the Web API, it is published as dll's, and when we have several controllers it is going to aggregate those into one dll.
But I have project that can have different controllers, like:
Android.cs
CoWorker.cs
TelegramBot.cs
and when I publish the project, I would like to publish the controllers in separated DLL's, because I should have update the controllers continuously and when I have an error in a specific controller, I can't publish another.
Please help!
What you are looking for will not happen on its own. If you want a plug and play like architecture where you can just publish only a module without re-publishing the whole solution.
To achieve this you need to move your controller(s)/module as a separate project (separate library). One of the useful configuration feature of ASP.NET Web API is that it allows you to define about the assemblies into which it will look to discover your controller types.
This is very useful if you have external assemblies and are not part of the Web API project.
You can implement interface System.Web.Http.Dispatcher.IAssembliesResolver GetAssemblies() or even can extend the DefaultAssembliesResolver as following.
public class CustomAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
//Your implementation here
}
}
You can find more details on this on following link.
MSDN
Sample
I think what you're looking for is a MICRO-SERVICE architecture ...
https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/data-driven-crud-microservice
https://github.com/dotnet-architecture/eShopOnContainers
you can use .NET full framework
or the new .NET Core (it depends on what you have to do)

How to use use same configuration between .NET Framework, .NET Standard, and .NET Core project?

Since the ConfigurationManager doesn't exist in .NET Standard, what is the best way to retrieve application settings for the executing assembly, whether it be a web.config or an appSettings.{env}.json, within a .NET standard class library?
We have three projects:
ASP.NET Core Web App (1.1)
ASP.NET Web App (4.6.1 )
.NET Standard Class Library (1.4) -> Referenced by both projects
In the .NET Standard class lib, we have a URL which needs to change based on the environment it's deployed to.
This is fine for the .NET Core app, we simply add the URLs to the appropriate appSettings.{env}.json file, add the appropriate values to the DI container, then use the Microsoft.Extensions.Options library to retrieve the configuration.
However, we also need to call the library from our .NET Framework 4.6.1 application, but since there is no method (at least none that I could find) to retrieve the values from a web.config file, we don't have a good way to set the URL within the class library.
We've gotten around this for now by making the URL variable static and setting its value from within the Application_Start() method of each .NET Framework project we reference it from, we determine which environment it is via an appSetting key we added to each web.config, then manually set the URL based on the environment.
Is there a more robust way to handle retrieving appSettings values from both a .NET Core and .NET Framework application within a .NET Standard class library?
Preferably where we can set the value solely within the class library.
You should read the value from configuration before calling your library, and pass it by parameter, either into a method or constructor.
In general, library code shouldn't depend on side effects from the configuration of its host environment. Doing so makes it very difficult to reuse code, or to test effectively.

Resources