Can you write to CRM 365 trace log from C# console app? - c#

I was wondering if it is possible to write to the trace logs of a CRM 365 org from within a C# console application. I can and have done this successfully with a plugin, but haven't been able to find much information on writing to the trace log from a console application.
Figured I'd ask here, before I just use the orgservice to create a record in an entity and add the data that way.

I don’t think you can do it.
plugintracelog EntityType is supporting GET & DELETE only, no POST support. Also every single attribute is read-only. That being said it is an internal only use entity. Especially for plugins & WFs.
So create a custom entity & log everything.
Btw, Still you can mock it by implementing interface ITracingService.

Although I have never used it, the Microsoft.Xrm.Tooling.Connector namespace has some ability to configure tracing via the TraceControlSettings class, and other trace-related classes that the first link shows.
However, the example in this post indicates that it's for external tracing (i.e. to a file), rather than using the plugin trace mechanism.
Another commentary: Use Trace not Console

Related

c# Finishing a BPF(Business Process Flow)

As mentioned in the blog below, could the attribute, _bpf_accountid_value be used when not found in the default solution? Trying something similar with leads, I am trapping the error "the given key not present in dictionary".
I am on the lead form and for testing purposes I traverse the BPF and without manually finishing it,
I am trying to make my custom workflow "Finish" it, on demand.
I hesitate to show my code because it is commented with various attempts and will only loose the focus of this specific question.
Please help.
https://community.dynamics.com/365/b/crmmemories/posts/finish-a-business-process-flow-in-c
I just verified in my instance as the BPF will have its own entity, I can find those attributes. You can check too using CRM REST Builder or the native Advanced find - download fetchxml.
There is a difference between OOB like Lead entity BPF and custom entity BPF like below: (_bpf_xxx_projectinspectionsid_value vs _leadid_value)
https://crmdev.crm.dynamics.com/api/data/v9.1/xxx_inspectionflows(00000000-0000-0000-0000-000000000000)?$select=_bpf_xxx_projectinspectionsid_value,bpf_name,businessprocessflowinstanceid
https://crmdev.crm.dynamics.com/api/data/v9.1/leadtoopportunitysalesprocesses(00000000-0000-0000-0000-000000000000)?$select=businessprocessflowinstanceid,_leadid_value

jBPM custom authorization

I am trying to use in jBPM users from existing ASP .NET MVC Web Site.
As I understand from docs and this forum topics (first, second) best solution would be implementing of UserGroupInfoProducer that will call external service. But due lacking of experience with java I faced with several problems.
First approach: Create project with required implementation, deploy it and config jBMP to use it.
Problem was in implement interfaces that declared in another project, I've tried to add maven dependencies but after failing with some classes I've just added reference to required jar.
Deploy it on jboss like war failed, deploying like jar succeeded but server did not find UserGroupInfoProducer and other implemented interfaces.
Another problem in changing config of jbmp-console. Only way that I've found for that is modify archive directly, but I don't this it's right solution.
Second approach: Create own package of jBPM with required classes.
Problem here that I don't know what repository use for this and how to build version for my server.
As I understand from this link I need to use jbmp-console-ng, only maven task for creating war package that I found was in jbpm-console-ng-showcase I've tried to run it (release 6.2.0 Final) on:
On Windows: failed to execute because of maven error about long path, after migrating project to gradle and excluding dependencies on jmxtools-1.2.1.jar and jmxri-1.2.1.jar it created war but jboss failed to start service.
On Mac and Ubuntu using virtual box: it required to downgrade java to 1.6 and built war after this, but it failed to deploy due duplication of some classes.
As I understand you need to build diferently for each version of server but I don't know how to do this.
Third approach: Create come simulation of supported authorization ways. As I understand jBMP support LDAP, JAAS, database and file. Database and files will require duplicating users so I researched about simulating LDAP or JAAS (preferably using C#) but did not find any acceptable way.
I will be very grateful for any advise which of this approach may work or some other suggestions. Especially about building war of jbmp-console.
version used:
jBPM(6.2.0 Final), jBoss(Wildfly 8.1.0 Final), Java(1.8.0.73), Ant(1.9.6), Gradle(2.11), Intellij IDEA (15.0.3).
After a few weeks of try and error approach I have finally managed to provide fully custom authorization module for JBPM suite ( kid-wb, server and dashbuilder ) in our application. It wasn't easy and required some magic - overwriting two classes won't do it :)
My requirements was quite complex and final solution consist of kie-wb, server, dashbuiler and external authentication which provide by REST Web Service response users with roles based on token passed in session. Another thing that you have to keep in mind is that kie-wb and server are communicating through BASIC authentication - if you want to use server also you have to provide two possible methods of authentication. I won't be able to publish here any code, because it is not an open source project, but I will try to help the best I can.
If you are using WildFly as you are saying above, what you should look at is Undertow Servlet Extension
Overwriting handleDeployment method allow you to write your own IdentityManager( if you need one ) and register your custom AuthenticationMechanism.
To implement your own AuthenticationMechanism you should look at this project Custom Spnego Auth for WildFly
My solution was based on mentioned above project - you don't have to implement every class - in my case writing my own class implementing AuthenticationMechanism was sufficient to get custom authentication working in kie-wb ( not for server though).
So if you already have overwritten Servlet Extension ( and registered by putting file io.undertow.servlet.ServletExtension containing your custom servlet extension class name inside /WEB-INF/classes/META-INF/services/ path of .war file ) and implemented custom AuthenticationMechanism next thing you should do is write class implementing org.jboss.security.auth.spi.LoginModule interface. If you don't want to implement this interface all by yourself you can just extend one of already implemented classes from WildFly - for example UsernamePasswordLoginModule or other.
To let WildFly know that we are using non-standard Login module we have to modify standalone-full.xml as follows:
<subsystem xmlns="urn:jboss:domain:security:1.2">
<security-domains>
<security-domain name="other" cache-type="default">
<authentication>
<login-module code="com.package.CustomAuth" flag="required">
</login-module>
</authentication>
</security-domain>
</security-domains>
</subsystem>
and then...we are almost done :) One thing left to do is to modify deployment descriptor inside .war file. We have to change web.xml inside /WEB-INF dir as follows:
<login-config>
<auth-method>BASIC?silent=true,CUSTOM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login_failed.jsp</form-error-page>
</form-login-config>
</login-config>
CUSTOM is name of your authentication mechanism that was registered inside class implementing Servlet Extension class.
Above instruction does not cover kie-wb <-> kie server communication. This matter was more complex and required a few workarounds. As I said before I won't be able to provide full solution with more detailed examples, but feel free to ask me anything refering this case.

Error logging sample project

I need to create a Error logging project from scratch in C#.
I would like to save to a file with several levels, this logging project I am taking as an assignment from which I can learn many things and want to build it as small loggin utility for now.
I saw few loggin project which has singleton pattern and a config file having some entries and also in the consuming application config - some references of logger proj interface are there
can some one please give me an idea as how can I create a new logger
proj from scratch and what is the purpose of having entries in
config ?
pseudo code for logger project or any link
Thanks in advance.
Instead of implementing your own logging mechanism you may want to check whether existing components are an option. For example log4net is a frequently used framework that people use for .NET based projects.
Also, the Logging Application Block from Microsoft:
http://msdn.microsoft.com/en-us/library/ff632023.aspx
http://msdn.microsoft.com/en-us/library/ff664569(v=PandP.50).aspx
There are several key elements you need to consider before making one from scratch. Just to name what comes to my head :
How do you want to log? Do you want to save logs to a file, in a database, to send mails, just to have the logs shown in a console?
If you persist the logs, do you want to log everything, forever, or you want a "rolling" X lines to be kept, the rest discarded?
Do you want to have several level of logs? For example, you could log some things Info, Warning, Error, Critical Error, etc.
Do you want your logging library to support custom formatting for the logs?
As for the question about the config, it's really something you want to do. If you're talking about the app.config files, it allows you to can change the configuration of your application without rebuilding it. It can also provide some default parameters the user can override. By user, I mean another developer using your library.

Create custom logger, should I extend class from log4net

I am doing something unusual.
I have an application, it runs as a windows service.
what it does is that, it monitor one folder, when ever there is some new file put into that folder, the application will do something to the file.
Whenever there is an error when processing one file. I need to create a text file, and put the error/exception information into that text file. (later i can do something with this file)
so there is something like this
FileWatch, when there is a new file, do following :
try
{
processing file
}
catch(Exception ex)
{
MyLogger write exception message into one new text file
}
So far how i did it is that. I create a class for example MyLogger, whenever i new one MyLogger, it creates a text file (the name matters, need to be a specific format), and there is one method in side MyLogger "WriteError(string message)", it writes text into that file.
Since i used log4net in my application. Do you think i should modify my logger, to extend some class from log4net, so that i can get some benefit? (not sure what kind of benefit i will get, but log4net is a good logging framework, the way it handle text file might have thing that i do not aware)
Thanks
log4net or any other generic logger is helpful if
1) you want to have a consistent logging facility in many places across your application; and/or
2) you want the ability to customize logging format, level and so on.
From your description it sounds like there is a single point in your app where you need to log the exception in a specific way. If this is correct, you will probably gain no benefit from creating a custom logger - just write a method that logs exception to a file in the way you need.
If I misunderstood you, and there is a need for generic logger (that is, either 1) or 2) above is true), extending log4net by inheriting a logger or creating a wrapper is fine.
I've created log4net wrappers before. I find it handy to start this way as you don't always know what the logging requirements are at the start of a project. My rule has been that the log4net library can only be referenced from my own "logging" namespace. This way, the application code only calls the wrapper, and the wrapper is the only point of contact to the log4net functionality.
In the long run, it's probably worth investing in building your own logger. If you encapsulate log4net properly, you should be able to make this upgrade rather easily, without having to change your code.
Why not use Trace Listeners from the .NET framework? They provide many of the benefits of a logging network, without the need to incorporate an external framework.
Benefits include centralized log management and the ability to direct the output logs to one or more sources such as a console window, text file, or the Windows Event Log.
You should spend some time creating your own logger that does exactly what you want. This would be the best way. Is also fairly easy and you have full control on the customization so you can make the output look and feel as in log4net. You could Google for logging sample and start modifying that one.
I am not sure if I would use a log framework for this purpose. I have the impression that writing this text file in the exception case is part of your business process. Logging serves a different purpose that can be turned off without affecting business processes...

Logging custom classes through WebService using NLog

Problem is as follows:
On log event I want to send my custom object (lets say LogMessage that wraps in some way logging event) to my web service. Could work like appenders in log4net, or is there this kind of thing in Nlog btw? Or how do I do this NLog way?
Note: I'm using WebService target wrapper (if this helps in any way).
[EDIT]
I have added some links rather than simply telling you where to look.
NLog has a LogReceiverService and a LogReceiverServiceTarget (these might have been added for NLog 2.0 which just went to Beta recently). From what I can tell, one way to use the service is to use NLog for logging in your app. Configure to send all logging messages to LogReceiverTarget. Configure LogReceiverTarget to point to the LogReceiverService. LogReceiverTarget will create "NLogEvents" from log messages and forward them to the LogReceiverService. LogReceiverService will convert "NLogEvents" back to LogEvents and log them via NLog. In other words, you are logging via NLog in your app and LogReceiverService is also logging via NLog.
I am posting from iPhone so it is harder for me to add links to the relevant NLog topics. Go to NLog website and look for documentation on LogReceiverService. Also look in the forum. There has been some traffic recently on LogReceiverService. Finally, if you google "nlog git" you will find NLog's git source repository (for LogReceiverService specifically). You might be able to learn something there.
Good luck!
I have an example using a wcf service (ILogReceiverServer), it might be useful
https://bitbucket.org/philipogorman/logreceiverserviceexample/src

Categories