示例#1
0
        public static bool Init()
        {
            // Verbose - tracing information and debugging minutiae; generally only switched on in unusual situations
            // Debug - internal control flow and diagnostic state dumps to facilitate pinpointing of recognized problems
            // Information - events of interest or that have relevance to outside observers; the default enabled minimum logging level
            // Warning - indicators of possible issues or service/functionality degradation
            // Error - indicating a failure within the application or connected system
            // Fatal - critical errors causing complete failure of the application
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] [{SourceContext}] {Message} {NewLine}")
                         .MinimumLevel.Debug()
                         // If you are using https://getseq.net
                         // .WriteTo.Seq("http://*****:*****@"\ApexSharp\config.json");

            try
            {
                // See if we have an existing connection
                ConnectionUtil.Session = ConnectionUtil.GetSession(@"\ApexSharp\config.json");
            }
            // Else Create a new session
            catch (SalesForceNoFileFoundException)
            {
                try
                {
                    // This example assumes you cloned your GIT repo to the root level.
                    ConnectionUtil.Session = new ApexSharp().SalesForceUrl("https://login.salesforce.com/")
                                             .AndSalesForceApiVersion(40)
                                             .WithUserId("You SF Id")
                                             .AndPassword("You SF Password")
                                             .AndToken("Token")
                                             .SalesForceLocation(@"\ApexSharp\SalesForce\src\")
                                             .VsProjectLocation(@"\ApexSharp\Demo\")
                                             .SaveConfigAt(@"\ApexSharp\config.json")
                                             .CreateSession();
                }
                catch (SalesForceInvalidLoginException ex)
                {
                    Console.WriteLine(ex.Message);
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        public static bool Init()
        {
            // Due to different OS and how they manage file paths, its best to to use the Path.GetFullPath Method.
            // This example assumes you cloned your GIT repo to the root level.

            // SessionLocation is where your SF session will be Saved
            var sessionLocation = Path.GetFullPath(@"../config.json");
            // SalesForceLocation is the location of your Salesofrce project
            var salesForceLocation = Path.GetFullPath(@"../SalesForce/src/");
            // VsProjectLocation is the location of your Visual Studio Project
            var vSprojectocation = Path.GetFullPath(@"../Demo/");

            try
            {
                // See if we have an existing connection use it.
                ConnectionUtil.Session = ConnectionUtil.GetSession(sessionLocation);
            }
            // Else Create a new session
            catch (SalesForceNoFileFoundException)
            {
                try
                {
                    ConnectionUtil.Session = new ApexSharp().
                                             SalesForceUrl("https://test.salesforce.com/") // your org....prod/dev is login.salesforce.com and sandbox is test.salesforce.com
                                             .AndSalesForceApiVersion(40)

                                             .WithUserId("YOUR USER ID")
                                             .AndPassword("YOUR PASSWORD")
                                             .AndToken("YOUR TOKEN")

                                             .SalesForceLocation(salesForceLocation)
                                             .VsProjectLocation(vSprojectocation)
                                             .SaveConfigAt(sessionLocation)
                                             .CreateSession();
                }
                catch (SalesForceInvalidLoginException ex)
                {
                    Console.WriteLine(ex.Message);
                    return(false);
                }
            }

            return(true);
        }
示例#3
0
        public static void InitializeSession()
        {
            // You need to have a JSON File name appsettings.json with your SF credential in your project.
            // See https://github.com/apexsharp/apexsharp/blob/master/README.md
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Path.GetDirectoryName(typeof(Program).Assembly.Location))
                          .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            var logFile = new FileInfo(Path.GetFullPath(configuration["LogFile"]));

            if (logFile.Directory.Exists)
            {
                // Make sure you enable logging. We use Serilog loggign library.
                // See https://github.com/serilog/serilog/wiki/Configuration-Basics
                Log.Logger = new LoggerConfiguration()
                             .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH-mm-ss.fff}:[{Level}]:[{SourceContext}]:{Message}:{NewLine}")
                             .WriteTo.File(logFile.FullName, rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH-mm-ss.fff}:[{Level}]:[{SourceContext}]:{Message}:{NewLine}")
                             .MinimumLevel.Verbose()
                             .CreateLogger();

                Log.ForContext <Setup>().Debug("Logging Started");


                var sessionFileInfo = new FileInfo(Path.GetFullPath(configuration["SessionFileInfo"]));

                // You can del the existing session if needed (Ex: you changed the Password), if you delete every time a new session is created which will delay the code.
                File.Delete(sessionFileInfo.FullName);


                if (sessionFileInfo.Exists)
                {
                    ConnectionUtil.Session = ConnectionUtil.GetSession(sessionFileInfo.FullName);
                }
                // Else Create a new session
                else
                {
                    try
                    {
                        var salesForceLocation = Path.GetFullPath(configuration["SalesForceLocation"]);
                        var vSprojectocation   = Path.GetFullPath(configuration["VsProjectocation"]);

                        new ApexSharp()
                        .SalesForceUrl("https://login.salesforce.com/")
                        .AndSalesForceApiVersion(40)
                        .WithUserId(configuration["SalesForceUserId"])
                        .AndPassword(configuration["SalesForcePassword"])
                        .AndToken(configuration["SalesForceToken"])
                        .SalesForceLocation(salesForceLocation)
                        .VsProjectLocation(vSprojectocation)
                        .SaveConfigAt(sessionFileInfo.FullName)
                        .CreateSession();

                        ConnectionUtil.Session = ConnectionUtil.GetSession(sessionFileInfo.FullName);
                    }
                    catch (FileNotFoundException ex)
                    {
                        Log.ForContext <Setup>().Debug(ex.Message);
                    }
                    catch (SalesForceInvalidLoginException ex)
                    {
                        Log.ForContext <Setup>().Debug(ex.Message);
                    }
                }
            }
            else
            {
                Console.WriteLine("Cant Find the path to the log location");
            }
        }