Created
September 2, 2018 04:16
-
-
Save danielplawgo/02852a0ced727620839a4dadfc2f611d to your computer and use it in GitHub Desktop.
Jak ponawiać operacje w .NET z wykorzystaniem Polly?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
outbound and ip.DstAddr >= 185.255.40.24 and ip.DstAddr <= 185.255.40.24 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
static void Main(string[] args) | |
{ | |
_logger.Info("Start"); | |
try | |
{ | |
var content = Download("http://plawgo.pl"); | |
_logger.Info("Success"); | |
} | |
catch (Exception ex) | |
{ | |
_logger.Fatal(ex, "Error"); | |
} | |
_logger.Info("End"); | |
} | |
static string Download(string url) | |
{ | |
var client = new WebClient(); | |
return client.DownloadString(url); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static string DownloadWithRetry(string url) | |
{ | |
var client = new WebClient(); | |
return Policy | |
.Handle<WebException>() | |
.Retry(3, (ex, retryCount) => | |
{ | |
_logger.Error(ex, $"Error - try retry (count: {retryCount})"); | |
}) | |
.Execute(() => client.DownloadString(url)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static string DownloadWithRetryAndDelay(string url) | |
{ | |
var client = new WebClient(); | |
return Policy | |
.Handle<WebException>() | |
.WaitAndRetry(new[] | |
{ | |
TimeSpan.FromSeconds(10), | |
TimeSpan.FromSeconds(20), | |
TimeSpan.FromSeconds(50) | |
}, (ex, timeSpan, retryCount, context) => | |
{ | |
_logger.Error(ex, $"Error - try retry (count: {retryCount}, timeSpan: {timeSpan})"); | |
}) | |
.Execute(() => client.DownloadString(url)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static string DownloadWithRetryForever(string url) | |
{ | |
var client = new WebClient(); | |
return Policy | |
.Handle<WebException>() | |
.RetryForever(ex => | |
{ | |
_logger.Error(ex, $"Error - try retry forever"); | |
}) | |
.Execute(() => client.DownloadString(url)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment