How to Create Log File in MSCRM using c#

Sometimes you might have the requirement to create log file while writing code either in Plugin or Custom Workflow or in web service etc to trace and keep the generated error.There might be a other option as well to keep it but here i am going to show Error Log in Text file.

In this blog I have described how to create log file in C#. Log file can be simple text fileXML document or we can also use tables in database to create log. Here I have used.txt file. In this file we have stored information about generated exception of application. Using this log file we have easily find out error list. In this file we have stored information according our requirement. For example in log file we have stored file nameexception namedate and timeerror line numberevent name, Controls name etc.

Now let’s to implementation

using System;
using System.IO;
using context = System.Web.HttpContext;

namespace LogText
    public class Logger

        public Logger(string ErrorCode, string ErrorMsg)

                string path = context.Current.Server.MapPath(\”~/ErrorLogging/\”);
                // check if directory exists
                if (!Directory.Exists(path))
                path = path + DateTime.Today.ToString(\”dd-MMM-yy\”) + \”.log\”;
                // check if file exist
                if (!File.Exists(path))
                // log the error now
                using (StreamWriter writer = File.AppendText(path))
                    string error = \”\\r\\nLog written at        : \” + DateTime.Now.ToString() +
                    \”\\r\\n\\r\\nError occured on page : \” + context.Current.Request.Url.ToString() +
                    \”\\r\\n\\r\\nError Code            : \” + ErrorCode +
                    \”\\r\\n\\r\\nError Message         : \” + ErrorMsg;

            catch (Exception ex)
                throw new Exception(ex.Message);

Where i would see the Error Log Text file ?

A folder will be created inside your Project/Solution named \’ErrorLogging\’. All error log test file will be created inside this Folder with named as \’Log Creation Date.log\’ for example- \’04-Jul-13.log\’.

Error Log File will look like as below:

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

One thought on “How to Create Log File in MSCRM using c#

  1. Special thanks to ( for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (, if not for him i will never know what has been going on for a long time. Contact him now and thank me later.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: