how to webservicetarget url dynamically in nlog for xamarin forms - xamarin

how to web service target URL dynamically in NLog for Xamarin.Forms in NLog.Config file. need to get a to a variable instead of hardcoding.
<target name="webservice"
xsi:type="WebService"
url="{var:url}"
protocol="JsonPost"
proxyType="NoProxy"
namespace="{var:namespace}"
methodName ="InsertLogs"
encoding="utf-8">
<parameter name="Datetime" type="System.String" layout="${time}"/>
<parameter name="level" layout="${level}" />
</target>

You can load and modify your target at runtime and set the URL:
var target = LogManager.Configuration.FindTargetByName("webservice") as WebServiceTarget;
target.Url = new Uri("https://my.url/logs");
LogManager.ReconfigExistingLoggers();

Notice that NLog v5.0 adds Layout-support for Url-option.
There are NLog v5.0 pre-release available for try out.
See also: https://github.com/NLog/NLog/wiki/WebService-target

Related

NuGet uses invalid feed when adding a new package

I've been using both a public and a private feed for NuGet and I've added them in a Nuget.Config file in my project's root:
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="ligetFeed" value="http://graymini.local:9011/api/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
Running nuget restore works fine, and restores packages from both nuget.org and my private feed, but while trying to add a new nuget from nuget.org, it fails because it uses the private feed instead of nuget.org. I've been trying to add it both from Visual Studio (Mac) and command line, via nuget install <package> but the same thing happens.
The output looks like this:
Feeds used:
https://api.nuget.org/v3/index.json
http://privatefeed.local:9011/api/v3/index.json
Installing package 'Polly' to '/Users/user/Documents/MyProject'.
CACHE https://api.nuget.org/v3/registration4-gz-semver2/polly/index.json
GET http://privatefeed.local:9011/api/v3/registration/polly/index.json
An error was encountered when fetching 'GET http://privatefeed.local:9011/api/v3/registration/polly/index.json'. The request will now be retried.
mono-io-layer-error (-1)
GET http://privatefeed.local:9011/api/v3/registration/polly/index.json
An error was encountered when fetching 'GET http://privatefeed.local:9011/api/v3/registration/polly/index.json'. The request will now be retried.
mono-io-layer-error (-1)
GET http://privatefeed.local:9011/api/v3/registration/polly/index.json
An error occurred while retrieving package metadata for 'Polly' from source 'ligetFeed'.
mono-io-layer-error (-1)
Notice it tries to use GET http://privatefeed.local:9011/api/v3/registration/polly/index.json instead of api.nuget.org
Are there any extra steps needed? How does NuGet decide which feed to use?
N.B. I've managed to add the package just fine by removing the private feed entry from Nuget.Config, adding the package, then re-adding the feed and restoring, but this doesn't seem right on the long term.
Unfortunately , there is no way to set the an OPTIONAL priority for feeds to solve that problem .I hope can do that as follow :
<packageSources>
<!-- Internal package source comes before NuGet.org proxy -->
<add key="my_private_feed" value="http://internal-server:8081/repository/private_feed1/" priority="1" />
<add key="cache_nugetorg" value="https://www.nuget.org/api/v2/" priority="2" />
</packageSources>
However , it can not support . From this issue link , I know that :
This is by design, sources are unordered and they are searched by order of the quickest response rather than by order of how they show up in file.
By the way , there is an unperfect way to set nuget.org disable by using activePackageSource and disabledPackageSources .
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Chuck Norris Feed" value="https://www.myget.org/F/chucknorris" />
</packageSources>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
<activePackageSource>
<add key="Chuck Norris Feed" value="https://www.myget.org/F/chucknorris" />
</activePackageSource>
</configuration>
However , this way also can't realize it. I also find this issue link , Priority methods have been proposed as suggestions .
Finally , I will look forward to such a method in the future.
Turns out it's a problem with mono and mDNS (.local). I've been using Mac OS for the Nuget Feed with LiGet and as you may know, you can access any other Mac on the network with either the IP or name.local, as I did with graymini.local
After replacing the feed in NuGet.Config from
<add key="ligetFeed" value="http://graymini.local:9011/api/v3/index.json" protocolVersion="3" />
to
<add key="ligetFeed" value="http://22.33.444.555:9011/api/v3/index.json" protocolVersion="3" />
Everything seems to work fine. Trying to obtain the new package from the private feed will return NotFound instead of erroring with mono-io-layer-error and it will continue just fine fetching it from nuget.org instead.
GET https://api.nuget.org/v3/registration4-gz-semver2/polly/index.json
GET http://22.33.444.555:9011/api/v3/registration/polly/index.json
NOTFOUND http://22.33.444.555:9011/api/v3/registration/polly/index.json
OK https://api.nuget.org/v3/registration4-gz-semver2/polly/index.json

How to load .gltf in a Blazor application

I am trying to load .gltf model using three.js into my Blazor application.
However the server does not serve this type of files.
I am aware that MIME type must be added, but for some reasons, that cannot be done with Blazor web app as the 'app' variable in Startup.cs is an instance of IComponentsApplicationBuilder. Can anybody help me with this issue.
IIS and IIS Express will not serve files with unknown extensions. In your error console, you see 404 (Not Found) which means either the file is missing, or the MIME type for the file is not registered.
I would recommend you try adding a web.config file to the root of your application, with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".gltf" />
<mimeMap fileExtension=".gltf" mimeType="model/gltf+json" />
<remove fileExtension=".glb" />
<mimeMap fileExtension=".glb" mimeType="model/gltf-binary" />
<remove fileExtension=".bin" />
<mimeMap fileExtension=".bin" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
The <remove ... /> statements are there to avoid any possible conflicts with any MIME type registrations that happen in parent folders or at the root-level or system-level. It's always safe to remove, but it's a configuration error to add one that already exists.
Here's a reference to where the glTF Mime Type was defined.
Some versions of IIS Express will disregard MIME types from web.config. If this happens, the above file may not work. In that case you may have to edit the IIS Express configuration file directly, to add the information shown above. Check this SO answer to see how to locate that config file.
There's no need to mess around with web.config.
You just need to inject the StaticFileOptions from Program.cs.
using Microsoft.AspNetCore.StaticFiles;
...
builder.Services.Configure<StaticFileOptions>(options =>
{
options.ContentTypeProvider = new FileExtensionContentTypeProvider
{
Mappings =
{
[".gltf"] = "model/gltf+json",
[".glb"] = "model/gltf-binary",
[".bin"] = "application/octet-stream"
}
};
});
Full docs with an alternative option:
https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-6.0#blazor-server-file-mappings-and-static-file-options

Unable to set $(ReplacableToken...) in Web.config when publishing a Web Project

I have a web project and I craeted a Test publish configuration. I use Web Deploy Package for publish method. My target is to have a replacable parameter for a setting in appSettings, so that on deployment a new setting value would be used in SetParameters.xml. I use parameters.xml file created in the root project folder with the following structure.
parameters.config
<parameters>
<parameter name="webApiUrl"
defaultValue="http://localhost:50594/">
<parameterEntry
kind="XmlFile"
scope="Web.config"
match="/configuration/appSettings/add[#key='webApiUrl']/#value" />
</parameter>
</parameters>
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="webApiUrl" value="http://localhost:50594/" />
</appSettings>
</configuration>
The problem is that when the project is published the webApiUrl setting is not changed to $(ReplacableToken_webApiUrl) so when the project is deployed the value in SetParameters.xml is not taken into consideration.
I used a lot of different variations of scope and match, but non of them worked.
scope="\Web.config$", "obj\x86\Test\Package\PackageTmp\Web.config$", "\Web.config$", "\web.config$"
When I publish the project I check the folder (ProjectRoot)\obj\x86\Test\Package\PackageTmp\Web.config to see if the parametrization works.

How do you add a mime type when using ASP.NET vNext?

There's LOADS of information on how to add MIME types into a normal project.
These include configuring IIS, or modifying the web.config.
Both of these options are unavailable to me in vNext with IIS Express.
I had a look at the schema to the project.json file and couldn't find anything in there that would help either.
Can this be done yet? - I want to add a mime type for the .woff2 extension.
If you hosting it on IIS 7 or later then following step will do what you need. This answer I have used Visual Studio 2015 CTP5.
Publish your web application ( ASP.net vnext)
You can publish it to location like C:\MyPublish
Once it get successfully published you will find following location C:\MyPublish\wwwroot. Here You will find web.config.
Now host your site to in IIS ( Make sure that you have used C:\MyPublish\wwwroot as your path)
Now edit web.config over here just like you did for old version to add mime type. ( Following is my edit)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\WebApplication5" />
</appSettings>
<system.webServer>
<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>
</system.webServer>
</configuration>
Note: As per my thinking In old version it is fix that it is always windows environment so we have direct web.config file in project and we edit that but now we have to follow different process to register as in future we can host completly on linux env as well.
Update : There is another way to do that as well. If you are using Microsoft.AspNet.StaticFiles package then you will have extension.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
}
This will indirectly use https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs. Here you can see all mapping.
Update 2: (Add New Mime Type)
public void Configure(IApplicationBuilder app)
{
StaticFileOptions option = new StaticFileOptions();
FileExtensionContentTypeProvider contentTypeProvider = (FileExtensionContentTypeProvider)option.ContentTypeProvider;
contentTypeProvider.Mappings.Add("<<yourextention>>","<<mimetype>>");
app.UseStaticFiles(option);
}
Until this is released, you can also edit applicationhost.config which I found in D:\Documents\IISExpress\config (yours might be on your C drive [Documents]).
I added:
<mimeMap fileExtension=".woff2" mimeType="font/x-woff2" />
Inside <staticContent>.

Log4Net output to Mongo

Does anyone had the luck to have this configured correctly?
Created an account on MongoHQ
Added a new user to the database
Created a new Collection named logs_net
Added log4mongo-net library
Added the configuration to the web.config
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
and
<log4net>
<appender name="MongoAppender" type="log4net.Appender.MongoDBAppender, log4mongo-net">
<!-- MongoDB connection options -->
<host value="staff.mongohq.com" />
<port value="10048" />
<databaseName value="d1741d63-46b1-4a44-9c21-8a85cecae45b" />
<collectionName value="logs_net" />
<userName value="balexandre" />
<password value="myPassWorD" />
</appender>
Added log4net.Config.XmlConfigurator.Configure(); to global.asax under Application_Start()
and added some info:
ILog logger = LogManager.GetLogger(this.GetType());
logger.Info("MainController Initialize test");
And... I can't get logs into MongoDB, any help?
By the way, the Database name is not the correct one, neither the password, and if I use log4net.Appender.RollingFileAppender it works great.
I suggest turning on internal debugging, this should reveal what is going wrong. It is quite possible that the log4mongo assembly is not loaded correctly. Are you sure it is copied (with all dependencies) to the bin folder?
I had the same symptoms for a different reason. My XML logging configuration was wrong. Here is how I found out.
1) Enable internal (not log4net) debugging in the code before logger is created:
log4net.Util.LogLog.InternalDebugging = true;
...
XmlElement conf = ...
XmlConfigurator.Configure(conf);
var logger = LogManager.GetLogger(loggerName);
2) Run your code and see output in Visual Studio Output window with Debug selected in Show output from dropdown. You should be able to see how Appenders are constructed.
In my case database url was wrong - I specified additional options in a bad format.

Resources