Evaluating complex integral $\int_{0}^{\pi} \frac {x \sin x}{1+a^2-2a(\cos x)} $ via different contour 
I got an complex integral $\int_{0}^{\pi} \frac {x \sin
> x}{1+a^2-2a(\cos x)} $ for $a \ge 1$ and my given contour is a
  rectangle such that $|Re(z)|\le \pi$ and $0 \le |Im(z)| \le h \to
 \infty$.

I can rewrite the integral as $\int_{0}^{\pi} \frac {x \sin x}{1+a^2-2a(\cos x)} =1/2\int_{- \pi}^{\pi} \frac {x \sin x}{1+a^2-2a(\cos x)} $ and use the substitution:
$z = e^{ix}$, $\;$ $\cos x = \frac {e^{ix}-e^{-ix}}{2} = \frac {z + \frac {1}{z}}{2}$,$\;$
$dz = ie^{ix}dx$
We then received the expression for $f(z)$ as
$\frac {z(-i)}{1+a^2-a(\frac {1}{z}+z)} $ and we want to find the singularities of this function.
After some algebra, we get that we have two singularities at $z = a$ $\;$ & $\;$$z = \frac {1}{a}$, which are poles of the first order.
To evaluate that integral we are going to use the residue theorem:
$res_a = \lim_{\to a} \frac {z(-i)}{(z-1/a)}= \frac {a(-i)}{(a-1/a)}$
$res_{1/a} = \lim_{\to 1/a} \frac {z(-i)}{(z-a)}=\frac {1/a(-i)}{(-a+1/a)}$
and then $I = 2i\pi (res_a+res_{1/a})$
But this looks like the result if we were integrating around unit circle and I don't understand how that different contour changes the result to $I = \frac {\pi}{2} \frac {Ln(a+1)}{Ln(a)}$, according to my textbook. 
I was thinking about plugging the residues back to $z = e^{ix}$, but I don't understand how do I get that '+1', etc.
There was posted the same integral, but I am interested in that different integration contour.
 A: We assume $a \gt 1$ for simplicity.  Evaluation of this integral is greatly simplified by using the fact that
$$\frac{\sin{x}}{1+a^2-2 a \cos{x}} = \operatorname{Im}{\left (\frac1{a-e^{i x}} \right )}$$
Thus the integral of interest is
$$\frac12 \operatorname{Im} \int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} $$
We may use Cauchy's theorem to evaluate the integral.  To wit, consider the complex integral:
$$\oint_{C} dz \,\frac{\log{z}}{z (z-a)}$$
where $C$ is the following contour:

where the outer circle is the unit circle, the inner circle has a radius $\epsilon$ that will approach zero.  There is a branch cut along the negative real axis - negative because the integral is over $[-\pi,\pi]$.
We may write the above complex integral as a sum of integrals as we parametrize the various pieces of the contour $C$:
$$\begin{align}\oint_{C} dz \,\frac{\log{z}}{z (z-a)} &= \int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} + e^{i \pi} \int_{1}^{\epsilon} dx \, \frac{\log{x}+i \pi}{x (x+a)}  \\ &+ i \epsilon \int_{\pi}^{-\pi} d\phi \, e^{i \phi} \, \frac{\log{\epsilon}+i \phi}{\left ( \epsilon e^{i \phi} \right ) \left ( \epsilon e^{i \phi} -a \right )} + e^{-i \pi} \int_{\epsilon}^1 dx \, \frac{\log{x}-i \pi}{x (x+a)}  \end{align}$$
By Cauchy's theorem, the complex integral is zero.  We also consider the limit as $\epsilon \to 0$.  In this limit, the third integral (about the small circle) behaves as $i (2 \pi/a) \log{\epsilon}$, and we may write
$$\begin{align}\int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} &= -i \frac{2 \pi}{a} \log{\epsilon} - i 2 \pi \int_{\epsilon}^1 dx \, \frac{dx}{x (x+a)} \\ &= -i \frac{2 \pi}{a} \log{\epsilon} - i \frac{2 \pi}{a} \int_{\epsilon}^{1} \frac{dx}{x} + i \frac{2 \pi}{a} \int_{\epsilon}^{1} \frac{dx}{x+a}\\ &= i \frac{2 \pi}{a} \log{\left ( \frac{1+a}{a} \right )}\end{align}$$
Note that the singular pieces in $\epsilon$ canceled.  With this, we may reconstruct the original integral using the pieces at the top.  The result is, for $a \gt 1$,

$$ \int_0^{\pi} dx \, \frac{x \sin{x}}{1+a^2-2 a \cos{x}} = \frac{\pi}{a} \log{\left ( \frac{1+a}{a} \right )} $$

I leave as an exercise for the reader what the value of the integral is when $|a| \lt 1$ or when $a \in \mathbb{C}$.
A: 
my Atempt by using Real Analysis :

$$I=\int_{0}^{\pi }\ \frac{xsin(x)}{1+a^{2}-2acos(x)} dx$$
By using Integration by parts we have :
$$let\ \ \ u=x\ \ \ \ \ ,\ dv=\frac{sin(x)}{1+a^{2}-2acos(x)}\\
\\
\\
Hence\ \  du=dx\ \ \ \ \ ,\ dv=\frac{1}{2a}ln\left ( 1+a^{2}-2acos(x) \right )$$
So we have :
$$I=\frac{x}{2a}ln\left ( 1+a^{2}-2acos(x) \right )-\frac{1}{2a}\int_{0}^{\pi }\ ln\left ( 1+a^{2}-2acos(x) \right )dx
\\
\\
\\
\therefore \ I=\frac{\pi }{a}\ ln\left ( 1+a \right )-\frac{1}{2a}\int_{0}^{\pi }\ ln\left ( 1+a^{2}-2acos(x) \right )dx$$
let we suppose :
$$S(a)=\int_{0}^{\pi }\ ln\left ( 1+a^{2}-2acos(x) \right )dx$$
$$\therefore S'(a)=\int_{0}^{\pi }\frac{-2cos(x)}{1+a^{2}-2acos(x)}dx\ \ \ \ , let\ t=tan\left ( \frac{x}{2} \right )\\
\\
\therefore S'(a)=-2\int_{0}^{\infty }\ \frac{\left ( \frac{1-t^{2}}{1+t^{2}} \right )}{1+a^{2}-2a\left ( \frac{1-t^{2}}{1+t^{2}} \right )}.\ \frac{2}{1+t^{2}}\ dt$$
it is easily to find :
$$S'(a)=\frac{2\pi }{a}\ \ \ \ \Rightarrow \ S(a)=2\pi ln(a)+c\ \ \ \ , where\ c=0\ \\
\\
\\
\therefore S=2\pi ln(a)\ \ $$
So we have  : $$\therefore I=\frac{\pi }{a}ln(a+1)-\frac{1}{2a}.\left ( 2\pi ln(a) \right )\\
\\
\\
\therefore I=\frac{\pi }{a}\ ln\left ( (a+1) ) - ln(a) \right )=\frac{\pi }{a}\ ln\left ( \frac{a+1}{a} \right )$$
