What did I do wrong in solving $\int\sec^{-1} x\,{dx}$? I used integration by parts:
let u=$\sec^{-1}\,x$, dv=dx,
then du=$\frac{1}{|x|\sqrt{x^2-1}}$, v=x.
I = $x\sec^{-1}\,x\;-\;\int\frac{x}{|x|\sqrt{x^2-1}}dx\\$
Integration of $\int\frac{x}{|x|\sqrt{x^2-1}}dx$:
Let x=$\sec\theta$, then dx = $\sec\theta\tan\theta\,d\theta$. $\theta\in(0, \frac{\pi}2)\cup(\frac{\pi}2, \pi)\\$.
$\int\frac{x}{|x|\sqrt{x^2-1}}dx\,=\,\int\frac{\sec\theta}{|\sec\theta|\sqrt{\sec^2\theta-1}}\sec\theta\tan\theta\,d\theta\\\qquad\qquad\qquad=\,\int\frac{\sec\theta}{|\sec\theta|}\frac{\tan\theta}{|\tan\theta|}\sec\theta\,d\theta$
When $\theta\in(0, \frac{\pi}2),\;\sec\theta=x\text{, which is}\gt0$. $\tan\theta\gt0\text{, and }\tan\theta=\sqrt{x^2-1}$,
$\int\frac{x}{|x|\sqrt{x^2-1}}dx\,=\,\int\frac{\sec\theta}{\sec\theta}\frac{\tan\theta}{\tan\theta}\sec\theta\,d\theta\\\qquad\qquad\qquad=\,\int\sec\theta\,d\theta\\\qquad\qquad\qquad=\,\ln|\sec\theta+\tan\theta|+c\\\qquad\qquad\qquad=\,\ln|x+\sqrt{x^2-1}|+c$.
Then, I = $x\sec^{-1}\,x\;-\;\ln|x+\sqrt{x^2-1}|+c$.
I know that this is the right answer, but as I continue, I get a different answer for $\theta\in(\frac{\pi}2, \pi)$.
When $\theta\in(\frac{\pi}2, \pi)$, $\sec\theta=x\text{, which is }\lt0$, $\tan\theta\lt0\text{, and }\tan\theta=-\sqrt{x^2-1}$,
$\int\frac{x}{|x|\sqrt{x^2-1}}dx\,=\,\int\frac{\sec\theta}{-\sec\theta}\frac{\tan\theta}{-\tan\theta}\sec\theta\,d\theta\\\qquad\qquad\qquad=\,\int\sec\theta\,d\theta\\\qquad\qquad\qquad=\,\ln|\sec\theta+\tan\theta|+c\\\qquad\qquad\qquad=\,\ln|x-\sqrt{x^2-1}|+c\\\qquad\qquad\qquad=\,-\ln|x+\sqrt{x^2-1}|+c$
Then, I = $x\sec^{-1}\,x\;\boldsymbol{+}\;\ln|x+\sqrt{x^2-1}|+c$
The answer is $x\sec^{-1}\,x\;\boldsymbol{-}\;\ln|x+\sqrt{x^2-1}|+c\\$, what did I do wrong in the last part?
 A: The anti-derivative valid over all domain $|x|\ge 1$ is obtained as follows
\begin{align}
\int \sec^{-1}x \ dx 
=&\  x\sec^{-1}x - \int \frac x{|x|\sqrt{x^2-1}}dx\\
 =&\ x\sec^{-1}x - \int \frac 1{\sqrt{|x|^2-1}}d(|x|)\\
 =&\  x\sec^{-1}x - \cosh^{-1}(|x|)\
 =x\sec^{-1}x - \ln\left(|x|+\sqrt{x^2-1}\right) 
\end{align}
Your or the cited answer is only valid for a sub-domain.
A: Note that the hyperbolic relation $y^2-2xy+1=1$ is equivalent to $y=x\pm\sqrt{x^2-1}$. And one of those branches, $y=x-\sqrt{x^2-1}$ is equivalent to $y=\frac{1}{x+\sqrt{x^2-1}}$ (if you rationalize its denominator). And once you take the logarithm of $\frac{1}{x+\sqrt{x^2-1}}$, you have the negative of the logarithm of $y=x+\sqrt{x^2-1}$.
So the two answers are related as they capture a certain portion of the relationship $y^2-2xy+1=1$.
However as another answer explains, given the context with the domain of $\sec^{-1}$, working with $y=x+\sqrt{x^2-1}$ makes sense for positive $x$, and working with $y=\left\lvert\frac{1}{x+\sqrt{x^2-1}}\right\rvert$ makes sense for negative $x$.
A: 
When $\theta\in(0, \frac{\pi}2),$
Then I = $x\sec^{-1}\,x\;-\;\ln|x+\sqrt{x^2-1}|+c$.

Small correction: $\theta\in[0, \frac{\pi}2).$ This corresponds to $x\ge1.$

When $\theta\in(\frac{\pi}2, \pi),$
Then I = $x\sec^{-1}\,x\;\boldsymbol{+}\;\ln|x+\sqrt{x^2-1}|+c$

Small correction: $\theta\in(\frac{\pi}2, \pi].$ This corresponds to $x\le-1.$

what did I
do wrong in the last part?

Nothing: you appear to have correctly shown that $$\int\operatorname{arcsec} x\,\mathrm dx= \begin{cases}x\operatorname{arcsec} x+\ln\left|x+\sqrt{x^2-1}\right|+C_1&\text{if }x\le-1;\\x\operatorname{arcsec} x-\ln\left|x+\sqrt{x^2-1}\right|+C_2&\text{if }x\ge1\end{cases}.$$
