Logging custom classes through WebService using NLog - c#

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

Related

How to use Microsoft.Extensions.Logging and logging properly in MVC5 Library

NET Developer I am currently working on a ASP NET MVC 5 library. The code will eventually be built into a DLL and shipped as a nuget package to be consumed else where.
My Problem: I am looking at how to have some method of logging in this file that is flexible and can be used by all the different users who consume it. After doing some research, it looks like most people who are working in some form of .NET have access to Microsoft.Extensions.Logging and ILogger. This seems to fit my requirements since it has logging for errors, informational, warnings, etc.
Is this how I properly use it? I currently have
var factory = new LoggerFactory();
ILogger logger = factory.CreateLogger("MyLogger");
// Now I can call the generic methods
log.LogInformation("This is an informational log");
Am I missing key functionality? Online I have seen individuals use code such as ILoggerFactory factory = new LoggerFactory().AddConsole(); but I do not have access to `AddConsole after instantiating a new LoggerFactory. I mostly just want a generic way to log events so that others can use my code and appropriate logs are created and I don't have to worry about custom implementation of logging, etc. I'm more focused on logging string messages.

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

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

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.

Is there some class that implements logging facility, that I can use?

I need to use logger in my application.
I looking for some class in .net framework that i can use for this purpose - and i did not found.
Is there some ready library in .net framework that i can use for organize simple log ?
When you were looking in the framework, did you not see or not like the System.Diagnostics.Trace [1] framework?
As already mentioned log4net [2] is a good framework, we use it extensively. You may also be interested in the Log facility in the Castle Windsor IoC framework [3].
[1] http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx
[2] http://logging.apache.org/log4net/
[3] http://www.castleproject.org/container/facilities/trunk/logging/index.html
There are many third-party libraries for this. Check out log4net for instance.
But if all you want is to write some lines to a file it may be enough to create a text file that you write to. In that case you can use System.IO.File.CreateText(filename) to get a StreamWriter that you can log to. Then use StreamWriter.WriteLine to write to the log. Finally you should Close the StreamWriter when you are done logging.
Add a TraceListener to your app.config and use System.Diagnostics.Trace.WriteLine to write to the log.
You can have separate listeners for writing to a text file, event log etc.
elmah is good option, please check
and log4net is another
http://www.codeproject.com/KB/aspnet/Log4Net_Configuration.aspx
NLog or log4net are the most popular ones. I have mostly worked with NLog and are pretty satisfied with it. These logging frameworks allow you to define different rules and targets for logging, changing these on runtime (for debugging an app in production environment, for example) as well as using reflection so that your log messages can include things like the name of the class the log is originating from.

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...

Categories