How to use DevExpress Logify with Postsharp 5.0 Logging along with Serilog - devexpress

DevExpress Logify supports SeriLog and logs all fatal erros to its server. According to their document when ever serilog logs an error it will automatically trigger Logify as follows
Log.Logger = new LoggerConfiguration()
.WriteTo.Seq("http://localhost:5341").MinimumLevel.Error()
.WriteTo.Console(LogEventLevel.Error)
.WriteTo.LogifyAlert()
.ReadFrom.AppSettings()
.CreateLogger();
its working fine when using Serilog but the moment I use postsharp the event is not logged.
LoggingServices.DefaultBackend = new SerilogLoggingBackend();
How can I solve this?

According to the aforementioned link, currently, it is necessary to access the LogifyAlert.Instance property before creating LoggerConfiguration to initialize a Logify engine inside a plugin.
LogifyAlert client = LogifyAlert.Instance;
...
Log.Logger = new LoggerConfiguration();

Related

How to read logs of my own ASP.NET project and its dependencies?

I created a ASP.NET 6, Blazor Server-side project. I injected a logger to the WeatherForecastService classes. I created a library project, and referenced it from the ASP.NET project. I created a object in that library in the ASP.NET project and passed a logger.
If I start it in debug mode in Visual Studio, the log messages from both projects are printed in the Output panel. That is good, but what I what I want to do is, in addition to that (that is, not disabling the log output in the Output panel of VS), show the logs within my ASP.NET project. For example, there can be a "Logs" page.
Probably there is no easy way to send log messages to the client browser in real-time, so I am going to poll the server at every second for new log messages. To do that, I think I have to get notified whenever a log message happens in the ASP.NET project. Not just for the logs from the ASP.NET project itself, but from the referenced project, too, just like VS's Output panel. Can I do that?
ASP.NET 6, Blazor Server-side project
namespace BlazorApp1.Data
{
public class WeatherForecastService
{
private readonly ILogger _logger;
public WeatherForecastService(ILogger<WeatherForecastService> logger, IServiceProvider sp)
{
_logger = logger;
var d = new Dog(sp.GetRequiredService<ILogger<Dog>>());
logger.LogInformation("WeatherForecastService created.");
}
}
Referenced "library" project
using Microsoft.Extensions.Logging;
namespace ClassLibrary1
{
public class Dog
{
public Dog(ILogger<Dog> logger)
{
logger.LogInformation("Dog created.");
}
}
}
Depending on where are you logging you are ether pooling the file or the database. For referenced projects and logs in them you need to implement logger and actually log data to the same file. or different one, but in service you need to fetch all data from all the files.
As far as i know default logger that is logging to console is just for that. You need to ether implement your own logging library or you can import serilog or log4net. And insted of using default ILogger in the same way you use your implementation witch logs data to ether file or db.
Microsoft link for implementing logging provider https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider

Audit.NET EntityFramework

I am trying to replace our own Audit system with Audit.NET. I have checked the documentation for Audit.EntityFramework, https://github.com/thepirat000/Audit.NET/tree/master/src/Audit.EntityFramework, and it is not clear to me where the configuration setup should be added. Also, lets say I am building a ASP.NET CORE RestFUL API and need to keep track of users making changes by extracting user information from a JWT, how would I set that up with Audit.EntityFramework?
In the documentation there is the follow code snippet to configure audits for orders and tracking users :
Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeExplicitMapper(m => m
.Map<Order, Audit_Order>()
.Map<OrderItem, Audit_OrderItem>()
.AuditEntityAction<IAudit>((evt, entry, auditEntity) =>
{
auditEntity.AuditDate = DateTime.UtcNow;
auditEntity.UserName = evt.Environment.UserName;
auditEntity.AuditAction = entry.Action; // Insert, Update, Delete
})
)
);
However, if I would add that in the Startup.cs it would not help track what each user is doing on each call made by different users on the different endpoints. Do you have any example of how I can do this using Audit.EntityFramework?
Thanks
Have you seen the main Audit.NET documentation?
Yes, the setup must be executed before any audit takes place, so on your start-up code should be fine.
If you need to add more information to the audit events you could use custom actions that also should be setup on your start-up code. That doesn't mean you have to set the value at the start-up, but you have to provide a way to get the value on the start-up. For example if you need something from the current HttpContext, you could get it from an HttpContextAccessor. For example:
public void Configure(IApplicationBuilder app, IHttpContextAccessor contextAccessor)
{
// ...
Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var httpContext = contextAccessor.HttpContext;
// Add a new field
scope.Event.CustomFields["CorrelationId"] = httpContext.TraceIdentifier;
// reuse an existing field
scope.Event.Environment.UserName = GetUserFromContext(httpContext);
});
}
Also, Audit.NET provides two libraries Audit.WebApi and Audit.MVC to audit Web API and/or MVC calls.
Also there is a dotnet new template exposed you could use to quickly create a minimal WebAPI/MVC project with audit enabled and using entity framework. For example:
dotnet new -i Audit.WebApi.Template
dotnet new webapiaudit -E

Override serilog properties based on source/context

Is there any way to override either a sink or a property in serilog based on source or context.
For instance, I have this setup in an asp.net mvc core 2 project:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
.Enrich.FromLogContext()
.WriteTo.Seq("https://url", apiKey: "sdfasdfasdfas")
However, i just added Hangfire to my application, and would like to either use a different apikey for my Seq sink, or push a modified property to the sink based on "Source"="Hangfire." - exactly like you can override the minimumlevel based on "Source"="Microsoft".
As far as I can tell I can't inject a separate logger for hangfire at configuration, since the configuration method doesn't take parameters and defaults to the static log:
services.AddHangfire(config => config.UseSerilogLogProvider()

"System.Net.HttpListener" not available in IAppBuilder properties

I am developing a web app with mix authentication (Owin Token Based and Windows authentication). I have implemented Owin token based authentication and want to implement windows authentication for the users which are marked as active directory users.
In Owin middleware, I want to get requesting user's windows username. I am getting object in OwinContext.Request.User.Identity. However, OwinContext.Request.User.Identity.Name is always blank string.
I found that I should add below lines in startup.cs:
var listener = (HttpListener)app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
However, I am getting key not found exception. "System.Net.HttpListener" is not present in Properties array. I have installed Microsoft.Owin.SelfHost, Microsoft.Owin.Host.HttpListener. However, I am still getting the error.
Any help is greatly appreciated.
Thanks,
GB
For me issue was that project was started as shared lib, not a web app.
Solution was to add a line into .cspro file after <ProjectGuid> line.
<ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Then you dont need to add HttpListener explicitly, just reload project and follow this instructions starting from Properties edition part.
Enabling Windows Authentication in Katana
You can access principal the same ways:
Get the user in an OWIN middleware:
public async Task Invoke(IDictionary<string, object> env)
{
OwinContext context = new OwinContext(env);
WindowsPrincipal user = context.Request.User as WindowsPrincipal;
//...
}
Get the user in a Web API Controller:
// In a web api controller function
WindowsPrincipal user = RequestContext.Principal as WindowsPrincipal;
UPD: List of Visual Studio Projects GUIDs

Setting Timeout value for Salesforce Web Service/API

The API for Salesforce is a web service, you set it up by downloading a WSDL file from Salesforce and adding the WSDL to your .NET project.
But I can't find anywhere to set the Timeout value.
Normally in a .NET Web Service there is a Timeout property for this (as described in this question), but I can't seem to find one in this case.
Having attached the WSDL to your .net App, you can configure the Timeout property on the proxy class like:
PartnerReference.SforceService partnerRef = new PartnerReference.SforceService();
partnerRef.Timeout = 30000;
partnerRef.UseDefaultCredentials = true;
partnerRef.Proxy = System.Net.WebRequest.DefaultWebProxy;
partnerRef.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
PartnerReference.LoginResult loginResult = partnerRef.login("Name", "Password");
I'm fairly sure that this will work for the Enterprise WSDL, too...

Resources